0

How to assign this 0x546F206A65737420707573747920706C696B0D0A to byte[] in C#?

The value was obtained from a database.

MickyD
  • 14,343
  • 6
  • 43
  • 67
Rafał Developer
  • 2,015
  • 9
  • 38
  • 71
  • 3
    If you're reading it as a string then [see here](http://stackoverflow.com/questions/321370/how-can-i-convert-a-hex-string-to-a-byte-array) – Barracuda Mar 09 '15 at 06:16
  • @Barracuda yes it`s working fine. Thanks for your time. – Rafał Developer Mar 09 '15 at 06:21
  • possible duplicate of [How do you convert Byte Array to Hexadecimal String, and vice versa?](http://stackoverflow.com/questions/311165/how-do-you-convert-byte-array-to-hexadecimal-string-and-vice-versa) – Sebastian Negraszus Mar 09 '15 at 06:25

1 Answers1

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;
}

Usage:

...
var bytesArray = HexStringToByteArray("FF1EAA");
...
dbvega
  • 3,296
  • 1
  • 16
  • 20