开发者

BuiltIn Function to Convert from Hex String to Byte

开发者 https://www.devze.com 2022-12-24 08:07 出处:网络
This question is similar to the one here. One can easily convert from hex string to byte via the following formula:

This question is similar to the one here.

One can easily convert from hex string to byte via the following formula:

    public static byte[] HexStringToBytes(string hex)
    {
        byte[] data = new byte[hex.Length /2];
        int j = 0;
        for (int i = 0; i < hex.Length; i+=2)
        {
            data[ j ] = Convert.ToByte(hex.Substring(i, 2), 16);
            ++j;
        }
        return data;
    }

But is th开发者_Go百科ere a built-in function ( inside .net framework) for this?


Remove 0x and then use byte.Parse(textRepresentation, System.Globalization.NumberStyles.HexNumber)


There is nothing directly, cause there are so many possibilites. Eg. hex, octal, binary, with preceding 0x or 0X, etc.

But this How to should give you some more easier possibilities by using System.Globalization.NumberStyles.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号