0

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

The value was obtained from a database.

Rafał Developer
  • 2,135
  • 9
  • 40
  • 72
  • 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
  • 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");
...
denys-vega
  • 3,522
  • 1
  • 19
  • 24