将硬盘驱动器序列字符串写入二进制文件

发布于 2024-11-10 00:12:22 字数 1157 浏览 4 评论 0原文

我有一个简单的函数,可以从 C:\ 驱动器中获取硬盘序列号并将其放入字符串中:

ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"C:\"");
disk.Get();
string hdStr = Convert.ToString(disk["VolumeSerialNumber"]);

然后我尝试将上面的字符串转换为 ASCII,然后将其写入二进制文件,问题是我我所遇到的是,当使用流写入器转换此字符串并保存文件,并在十六进制编辑器中打开文件时,我看到了我最初想要写入的更多字节,例如“16342D1F4A61BC”

将显示为:08 16 34 2d 1f 4a 61 c2 bc

它以某种方式添加了 08 和 c2...

更完整的版本如下:

string constructor2 = "16342D1F4A61BC";
string StrValue = "";

while (constructor2.Length > 0)
{
    StrValue += System.Convert.ToChar(System.Convert.ToUInt32(constructor2.Substring(0, 2), 16)).ToString();
    // Remove from the hex object the converted value
    constructor2 = constructor2.Substring(2, constructor2.Length - 2);
}

FileStream writeStream;
try
{
    writeStream = new FileStream(Path.GetDirectoryName(Application.ExecutablePath) + "\\license.mgr", FileMode.Create);
    BinaryWriter writeBinay = new BinaryWriter(writeStream);
    writeBinay.Write(StrValue);
    writeBinay.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

谁能帮助我理解这些是如何添加的?

I have a simple function that grabs the hard drive serial number from the C:\ drive and puts it into a string:

ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"C:\"");
disk.Get();
string hdStr = Convert.ToString(disk["VolumeSerialNumber"]);

I'm then attempting to convert the string above into ASCII and then write it out to a binary file, the issue I'm having is, when converting this string and saving the file using streamwriter, and opening the file in a hex editor, I'm seeing more bytes that I originally wanted to write so for example "16342D1F4A61BC"

Will come out as: 08 16 34 2d 1f 4a 61 c2 bc

It's adding the 08 and c2 in there somehow...

The more complete version is as follows:

string constructor2 = "16342D1F4A61BC";
string StrValue = "";

while (constructor2.Length > 0)
{
    StrValue += System.Convert.ToChar(System.Convert.ToUInt32(constructor2.Substring(0, 2), 16)).ToString();
    // Remove from the hex object the converted value
    constructor2 = constructor2.Substring(2, constructor2.Length - 2);
}

FileStream writeStream;
try
{
    writeStream = new FileStream(Path.GetDirectoryName(Application.ExecutablePath) + "\\license.mgr", FileMode.Create);
    BinaryWriter writeBinay = new BinaryWriter(writeStream);
    writeBinay.Write(StrValue);
    writeBinay.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

Can anyone help me understand how these are getting added in?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黄昏下泛黄的笔记 2024-11-17 00:12:22

尝试一下:

string constructor2 = "16342D1F4A61BC";
File.WriteAllBytes("test.bin", ToBytesFromHexa(constructor2));

使用以下辅助例程:

public static byte[] ToBytesFromHexa(string text)
{
    if (text == null)
        throw new ArgumentNullException("text");

        List<byte> bytes = new List<byte>();
    bool low = false;
    byte prev = 0;

    for (int i = 0; i < text.Length ; i++)
    {
        byte b = GetHexaByte(text[i]);
        if (b == 0xFF)
            continue;

        if (low)
        {
            bytes.Add((byte)(prev * 16 + b));
        }
        else
        {
            prev = b;
        }
        low = !low;
    }
    return bytes.ToArray();
}

public static byte GetHexaByte(char c)
{
    if ((c >= '0') && (c <= '9'))
        return (byte)(c - '0');

    if ((c >= 'A') && (c <= 'F'))
        return (byte)(c - 'A' + 10);

    if ((c >= 'a') && (c <= 'f'))
        return (byte)(c - 'a' + 10);

    return 0xFF;
}

Try this:

string constructor2 = "16342D1F4A61BC";
File.WriteAllBytes("test.bin", ToBytesFromHexa(constructor2));

With the following helper routines:

public static byte[] ToBytesFromHexa(string text)
{
    if (text == null)
        throw new ArgumentNullException("text");

        List<byte> bytes = new List<byte>();
    bool low = false;
    byte prev = 0;

    for (int i = 0; i < text.Length ; i++)
    {
        byte b = GetHexaByte(text[i]);
        if (b == 0xFF)
            continue;

        if (low)
        {
            bytes.Add((byte)(prev * 16 + b));
        }
        else
        {
            prev = b;
        }
        low = !low;
    }
    return bytes.ToArray();
}

public static byte GetHexaByte(char c)
{
    if ((c >= '0') && (c <= '9'))
        return (byte)(c - '0');

    if ((c >= 'A') && (c <= 'F'))
        return (byte)(c - 'A' + 10);

    if ((c >= 'a') && (c <= 'f'))
        return (byte)(c - 'a' + 10);

    return 0xFF;
}
伪心 2024-11-17 00:12:22

尝试使用 System.Text.Encoding.ASCII .GetBytes(hdStr) 获取表示 ASCII 字符串的字节。

Try using System.Text.Encoding.ASCII.GetBytes(hdStr) to get the bytes that represent the string in ASCII.

晒暮凉 2024-11-17 00:12:22

文件中的字节序对您来说有多重要?

也许你可以使用类似的东西:

byte[] b = BitConverter.GetBytes(Convert.ToUInt32(hdStr, 16));

How important is endian-ness to you in the file?

Perhaps you can use something like:

byte[] b = BitConverter.GetBytes(Convert.ToUInt32(hdStr, 16));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文