convert hex string to byte[]

  • Thread starter Thread starter alexia
  • Start date Start date
A

alexia

Hi All,

I have the following hex string "03828710073100A22300";

I want to convert it to byte array in the following way:

"03" to 3
"82" to 82
"87" to 87
"10" to 10
"07" to 07
the byte array should look like {3,82, 87.10. 7....};

Thank you for your help.
 
I have the following hex string "03828710073100A22300";

I want to convert it to byte array in the following way:

"03" to 3
"82" to 82
"87" to 87
"10" to 10
"07" to 07
the byte array should look like {3,82, 87.10. 7....};

Okay, lots of problems with this post. First, in the array example above, I
have to assume your finger drifted from the comma to the period and those
two periods are supposed to be commas.

Second, 10 in hex (0x10) is NOT the same as the number "ten," which is what
is implied by what you wrote above in the array sample. Did you mean to
write {3, 0x82, 0x87, 0x10, 7} ?

Third, assuming you really DO have a string of hex digits (and the only part
of your example that suggests you do is the "A"), then all you have to do is
substring through the string in chunks of two characters and pass those
substrings to the byte.TryParse() method. Use the overload that takes a
NumberStyles argument and pass NumberStyles.AllowHexSpecifier.
 
alexia said:
I have the following hex string "03828710073100A22300";

I want to convert it to byte array in the following way:

"03" to 3
"82" to 82
"87" to 87
"10" to 10
"07" to 07
the byte array should look like {3,82, 87.10. 7....};

Assuming that you mean hex as in the subject line (and not
decimal as in the example), then:

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

Arne
 
Arne Vajhøj said:
alexia said:
I have the following hex string "03828710073100A22300";

I want to convert it to byte array in the following way:

"03" to 3
"82" to 82
"87" to 87
"10" to 10
"07" to 07
the byte array should look like {3,82, 87.10. 7....};

Assuming that you mean hex as in the subject line (and not
decimal as in the example), then:

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


Why are you dividing and multplying by 3? Am I missing something?
 
Jeff said:
Arne Vajhøj said:
alexia said:
I have the following hex string "03828710073100A22300";

I want to convert it to byte array in the following way:

"03" to 3
"82" to 82
"87" to 87
"10" to 10
"07" to 07
the byte array should look like {3,82, 87.10. 7....};
Assuming that you mean hex as in the subject line (and not
decimal as in the example), then:

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


Why are you dividing and multplying by 3? Am I missing something?


Ooops.

That code assumes dashes between bytes.

Replace 3 with 2 for the format specified by the poster.

My apologies for the goof.

Arne
 
alexia said:
Hi All,

I have the following hex string "03828710073100A22300";

I want to convert it to byte array in the following way:

"03" to 3
"82" to 82
"87" to 87
"10" to 10
"07" to 07
the byte array should look like {3,82, 87.10. 7....};

Thank you for your help.

There's a one-liner for everything. :)

byte[] data =
Regex.Matches("03828710073100A22300", "..")
.OfType<Match>()
.Select(m => Convert.ToByte(m.Value, 16))
.ToArray();
 
Back
Top