将硬盘驱动器序列字符串写入二进制文件
我有一个简单的函数,可以从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试一下:
使用以下辅助例程:
Try this:
With the following helper routines:
尝试使用 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.
文件中的字节序对您来说有多重要?
也许你可以使用类似的东西:
How important is endian-ness to you in the file?
Perhaps you can use something like: