convert to array of byte

P

PawelR

Hello group,
I've string :"A1 10 15 06 87 FF 10" etc. this are hex value of character.
How convert this string to array of byte :

System.Byte[] arrBytes = new byte[50];

Thx
PawelR
 
L

Lars Moastuen

Check out String.Split() to split the string into array of HEX-numbers
{"A1", "10",...} and use Byte.Parse() to convert to byte:

Byte b = Byte.Parse("A1", System.Globalization.NumberStyles.HexNumber); //
b = 161

Lars Moastuen


Hello group,
I've string :"A1 10 15 06 87 FF 10" etc. this are hex value of character.
How convert this string to array of byte :

System.Byte[] arrBytes = new byte[50];

Thx
PawelR
 
M

Morten Wennevik

Hi PawelR,

As Lars said, you can do it with String.Split and then parse the value.

An alternate way which basically does exactly the same is

string s = "A1 10 15 06 87 FF 10";

string[] sa = s.Split(' ');
byte[] b = new byte[sa.Length];

for(int i = 0; i < sa.Length; i++)
b = Convert.ToByte(sa,16);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top