9

I'm trying to convert this string array to byte array.

string[] _str= { "01", "02", "03", "FF"}; to byte[] _Byte = { 0x1, 0x2, 0x3, 0xFF};

I have tried the following code, but it does not work. _Byte = Array.ConvertAll(_str, Byte.Parse);

And also, it would be much better if I could convert the following code directly to the byte array : string s = "00 02 03 FF" to byte[] _Byte = { 0x1, 0x2, 0x3, 0xFF};

2
  • 2
    I assume all the strings describe hexadecimal numbers? Commented May 10, 2012 at 9:30
  • 1
    yeah, it missing on the str part :) Commented May 10, 2012 at 9:40

6 Answers 6

14

This should work:

byte[] bytes = _str.Select(s => Convert.ToByte(s, 16)).ToArray();

using Convert.ToByte, you can specify the base from which to convert, which, in your case, is 16.

If you have a string separating the values with spaces, you can use String.Split to split it:

string str = "00 02 03 FF"; 
byte[] bytes = str.Split(' ').Select(s => Convert.ToByte(s, 16)).ToArray();
Sign up to request clarification or add additional context in comments.

5 Comments

@AhmadHafiz cool :) Also added a solution for converting a string. You can mark my response as answer if it helped you :)
I know this thread is old but I got a problem here. If i convert my string[] to byte[] I get for example [0] "70" string [1] "34" string [2] "0A" string to [0] 112 byte [1] 52 byte [2] 10 byte But I need the same characters as byte.
@Noli they are the same. "0A" is 10 in hexadecimal notation. It's just displayed in decimal format. The value is correct.
Ohhhh that is so.... I spent hours... But thank you sooo much for this!! Thank you!
Your code struck with unable to parse... this solution worked fine. stackoverflow.com/a/11268578/4425004
4

Try using LINQ:

byte[] _Byte = _str.Select(s => Byte.Parse(s)).ToArray()

1 Comment

Won't help. His problem is that Byte.Parse will not accept a string in hexadecimal format.
2

With LINQ is the simplest way:

byte[] _Byte = _str.Select(s => Byte.Parse(s, 
                                           NumberStyles.HexNumber,
                                           CultureInfo.InvariantCulture)
                          ).ToArray();

If you have a single string string s = "0002FF"; you can use this answer

Comments

1

You can still use Array.ConvertAll if you prefer, but you must specify base 16. So either

_Byte = Array.ConvertAll(_str, s => Byte.Parse(s, NumberStyles.HexNumber));

or

_Byte = Array.ConvertAll(_str, s => Convert.ToByte(s, 16));

2 Comments

Didn't know of that solution. Nice :)
Of course you can use string[] _str = stringWithSpaces.Split(' '); to create _str in the first place, as others have pointed out.
1

If you want to use ConvertAll you could try this:

byte[] _Byte = Array.ConvertAll<string, byte>(
    _str, s => Byte.Parse(s, NumberStyles.AllowHexSpecifier));

Comments

0

Try this one:

var bytes = str.Select(s => Byte.Parse(s, NumberStyles.HexNumber)).ToArray();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.