ASCII to hex/binary

  • Thread starter Thread starter TNCoder
  • Start date Start date
T

TNCoder

I have an ASCII string. For example:
string myString = "1200B2C4D2BB9863";
I need to convert this to byte[] 0x1200B2C4D2BB9863;

Are there any suggestions?
 
I have an ASCII string. For example:
string myString = "1200B2C4D2BB9863";
I need to convert this to byte[] 0x1200B2C4D2BB9863;

Are there any suggestions?

First suggestion is to make your problem statement less ambiguous.
There's no such thing as "byte[] 0x1200B2C4D2BB9863". Without spaces,
that implies a single hexadecimal integer. While a single hex integer can
be stored as a string of bytes (e.g. "byte[]"), it depends on byte
ordering. So "0x1200B2C4D2BB9863" can either be "0x12, 0x00, 0xB2, 0xC4,
0xD2, 0xBB, 0x98, 0x63" or the reverse.

You don't say where the string comes from, which makes it doubly
impossible for us to know for sure what the correct order is.

With that in mind, you may find the following a useful example as a
starting place:

byte[] ParseByteString(string strHexBytes)
{
List<byte> lb = new List<byte>(strHexBytes.Length / 2);

for (int ich = 0; ich < strHexBytes.Length; ich += 2)
{
lb.Add(byte.Parse(strHexBytes.Substring(ich, 2),
NumberStyles.HexNumber));
}

return lb.ToArray();
}

That code assumes the hex digit pairs occur in the same order as in your
desired byte[]. You'll want to run the loop backwards if you're dealing
with an actual hex integer in little-endian order that you want stored in
a byte[].

Pete
 
TNCoder said:
I have an ASCII string. For example:
string myString = "1200B2C4D2BB9863";
I need to convert this to byte[] 0x1200B2C4D2BB9863;

Are there any suggestions?

Straight:

public static byte[] XParse(string s)
{
byte[] ba = new byte[s.Length / 2];
for(int i = 0; i < ba.Length; i++)
{
ba = byte.Parse(s.Substring(2 * i, 2),
NumberStyles.HexNumber);
}
return ba;
}

Arne
 
Thanks for your help!

Terry

Peter Duniho said:
I have an ASCII string. For example:
string myString = "1200B2C4D2BB9863";
I need to convert this to byte[] 0x1200B2C4D2BB9863;

Are there any suggestions?

First suggestion is to make your problem statement less ambiguous.
There's no such thing as "byte[] 0x1200B2C4D2BB9863". Without spaces,
that implies a single hexadecimal integer. While a single hex integer can
be stored as a string of bytes (e.g. "byte[]"), it depends on byte
ordering. So "0x1200B2C4D2BB9863" can either be "0x12, 0x00, 0xB2, 0xC4,
0xD2, 0xBB, 0x98, 0x63" or the reverse.

You don't say where the string comes from, which makes it doubly
impossible for us to know for sure what the correct order is.

With that in mind, you may find the following a useful example as a
starting place:

byte[] ParseByteString(string strHexBytes)
{
List<byte> lb = new List<byte>(strHexBytes.Length / 2);

for (int ich = 0; ich < strHexBytes.Length; ich += 2)
{
lb.Add(byte.Parse(strHexBytes.Substring(ich, 2),
NumberStyles.HexNumber));
}

return lb.ToArray();
}

That code assumes the hex digit pairs occur in the same order as in your
desired byte[]. You'll want to run the loop backwards if you're dealing
with an actual hex integer in little-endian order that you want stored in
a byte[].

Pete
 
Back
Top