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};
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();
                        Try using LINQ:
byte[] _Byte = _str.Select(s => Byte.Parse(s)).ToArray()
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With