如何在C#中将此0x546F206A65737420707573747920706C696B0D0A
分配给byte[]
?
该值来自数据库。
答案 0 :(得分:0)
public static byte[] HexStringToByteArray(string hexString)
{
if (string.IsNullOrWhiteSpace(hexString))
throw new ArgumentNullException("hexString");
if (hexString.Length%2 != 0)
throw new Exception("Invalid hex string");
var bytes = new byte[hexString.Length/2];
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = Convert.ToByte(hexString.Substring(i*2, 2), 16);
}
return bytes;
}
用法:
...
var bytesArray = HexStringToByteArray("FF1EAA");
...