在C#中将byte []表单数据库分配给byte []

时间:2015-03-09 06:10:57

标签: c#

如何在C#中将此0x546F206A65737420707573747920706C696B0D0A分配给byte[]

该值来自数据库。

1 个答案:

答案 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");
...