1

I have a file containing some data (for example, "00927E2B112DB958......"). This data is a representation of bytes in ASCII form. The bytes are 8 bit, so 2 ASCII chars map to each byte that needs to go into the final output buffer array.

What is the best way to do this?

EDIT: What I am trying to do is go from a string that looks like "00DFFF" to a byte array of {0x00, 0xDF, 0xFF}, for example. I guess this wasn't clear.

Thanks!

chris12892
  • 1,634
  • 2
  • 18
  • 36

1 Answers1

5
private ICollection<byte> HexString2Ascii(string hexString)
{
    var bytes = new List<byte>(hexString.Length / 2);
    for (int i = 0; i <= hexString.Length - 2; i += 2)
        bytes.Add(byte.Parse(hexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber));
    return bytes;
}
Tim S.
  • 55,448
  • 7
  • 96
  • 122