Graviton
Graviton

Reputation: 83254

BuiltIn Function to Convert from Hex String to Byte

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 there a built-in function ( inside .net framework) for this?

Upvotes: 2

Views: 523

Answers (2)

Anonymous
Anonymous

Reputation: 9648

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

Upvotes: 3

Oliver
Oliver

Reputation: 45101

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.

Upvotes: 0

Related Questions