convert hex string to byte[]

  • Follow


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.
0
Reply alexia 11/25/2009 2:49:50 PM

"alexia" <alexia.bee@gmail.com> wrote in message 
news:24ad9d98-a2b1-4625-8521-42ae5b5edd73@l13g2000yqb.googlegroups.com...

> 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. 


0
Reply Jeff 11/25/2009 3:04:28 PM


alexia wrote:
> 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[i] = byte.Parse(s.Substring(3 * i, 2), 
NumberStyles.HexNumber);
             }
             return ba;
         }

Arne
0
Reply ISO 11/25/2009 10:13:53 PM

"Arne Vajh�j" <arne@vajhoej.dk> wrote in message 
news:4b0dac19$0$270$14726298@news.sunsite.dk...
> alexia wrote:
>> 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[i] = byte.Parse(s.Substring(3 * i, 2), 
> NumberStyles.HexNumber);
>             }
>             return ba;
>         }

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


0
Reply Jeff 11/25/2009 10:23:55 PM

Jeff Johnson wrote:
> "Arne Vajh�j" <arne@vajhoej.dk> wrote in message 
> news:4b0dac19$0$270$14726298@news.sunsite.dk...
>> alexia wrote:
>>> 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[i] = 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
0
Reply ISO 11/25/2009 10:32:15 PM

alexia wrote:
> 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();

-- 
G�ran Andersson
_____
http://www.guffa.com
0
Reply ISO 11/27/2009 11:30:06 AM

5 Replies
442 Views

(page loaded in 0.186 seconds)

Similiar Articles:
















7/18/2012 4:18:36 PM


Reply: