【发布时间】:2017-09-09 06:44:35
【问题描述】:
我正在尝试将字符串十六进制值转换为“二进制”(.bin) 格式的解决方案, 比如 xxd 命令。
示例: 我有一个字符串 hexValue "17c7dfef853adcddb2c4b71dd8d0b3e3363636363"
在 linux 上,我想要一个类似下一个的结果:
echo "hexvalue" > file.hex cat 文件.hex | xxd -r -p > 文件.bin
file.bin 的 hexdump 给出:
0000000 17 c7 df ef 85 3a dc dd b2 c4 b7 1d d8 d0 b3 e3
0000010 36 36 36 36 36 36 36
我的转换程序是:
Private static string convertHex(string value)
{
string[] hexVal = value.Split(' ');
StringBuilder s = new StringBuilder();
foreach (string hex in hexVal)
{
int val = Convert.ToInt32(hex, 16);
string stringValue = char.ConvertFromUtf32(val);
s.Append(stringValue);
}
return s.ToString();
}
string MyString = "17 c7 df ef 85 3a dc dd b2 c4 b7 1d d8 d0 b3 e3 36 36 36 36 36";
string newString = converthex(MyString);
Console.WriteLine(newString);
File.WriteAllText("file2.bin", newString);
所以现在,当查看 file2.bin 的 hexdump 时,我看到了:
0000000 17 c3 87 c3 97 c3 af c2 85 3a c3 9c c3 9d c2 b2
0000010 c3 84 c2 b7 1d c3 98 c3 90 c2 b3 a3 36 36 36
为什么我的新文件中存在 c3 或 c2 ? 你有解决办法吗?
感谢您的帮助!
【问题讨论】: