我想一个字符串(“00812B1FA4BEA310199D6E00DD010F5402000001807”)转换为字节数组。 但我希望字符串的每个数字都是十六进制值。转换十六进制字符串(字节列表)字节数组
预期的结果:
array[0] = 0x00;
array[1] = 0x81;
array[0] = 0x2B;
array[0] = 0x1F;
etc...
我试了几种方法。没有给出预期的结果。最接近的人是:
private static byte[] Convert(string tt)
{
byte[] bytes2 = new byte[tt.Length];
int i = 0;
foreach (char c in tt)
{
bytes2[i++] = (byte)((int)c - 48);
}
return bytes2;
}
public static byte[] ConvertHexStringToByteArray(string hexString)
{
byte[] HexAsBytes = new byte[hexString.Length/2];
for (int index = 0; index < HexAsBytes.Length; index++)
{
string byteValue = hexString.Substring(index * 2, 2);
byte[] a = GetBytes(byteValue);
HexAsBytes[index] = a[0];
}
return HexAsBytes;
}
简单的谷歌搜索将给你几个解决方案,其中任何一个工作。 –
对于那些正在写简单的谷歌搜索的人会给你解决方案...做搜索,如果它的工作发布使用的关键字。自从几个小时后,我开始使用Google搜索解决方案。 – CloudAnywhere
我已经做了搜索,发现了几场比赛,测试了它们。我也修复了你的代码,只需要2个简单的小修改,但是,我仍然会把它作为一个副本投票。简单的'byte a = System.Convert.ToByte(byteValue,16); HexAsBytes [index] = a;' –