4

Possible Duplicate:
How do you convert Byte Array to Hexadecimal String, and vice versa, in C#?

I have a textbox that gets in input the string "AA 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF", i split it to have the String[], but now i have to get a byte[] like this:

byte[] b6 = new byte[20] {0xAA,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88 ,0x99 ,0xAA ,0xBB,0xCC ,0xDD ,0xEE,0xFF};

can someone suggest me how to do it. I tried to use Convert.ToByte but I get the error of impossible to convert String to byte. And i don't have to convert the values in hex, only to add the 0x in front of each byte and add to the byte array.

Community
  • 1
  • 1
user1788654
  • 311
  • 2
  • 5
  • 13
  • it's not a duplicate because i tried to apply the things said in that post but i didn't get what i need. If I upply the 16 parse or Hex parse i get other values, not the format 0x but array byte with values like "170,17,..." so it convert the 0x11 in 17 decimal but i want to get 0x11. – user1788654 Oct 31 '12 at 13:30
  • @user1788654, see my answer, I explained how to display bytes as you want – tukaef Oct 31 '12 at 13:41

4 Answers4

2
string input = "AA 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF";
byte[] bytes = input.Split().Select(s => Convert.ToByte(s, 16)).ToArray();
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • I tried this solution, but i don't get what i want, i get a byte array with values like "170,17,..." so it convert 11 to 17. But i want to get 0x11. I know that 11 is 17 in decimal but i need the hex format to get in input to a reader. – user1788654 Oct 31 '12 at 13:31
  • I need to get in input: "AA 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF" and in output byte array with the same values, only with the "0x" in fronto of each bit. – user1788654 Oct 31 '12 at 13:34
  • 1
    @user1788654, 0x11 is the same as 17... they're just different representation of the same value (hexadecimal and decimal). It doesn't make sense to say that you want it as 0x11; this is just a notation used in code, at runtime the value isn't represented as either 0x11 or 17, but in binary (00010111). What exactly are you trying to achieve? – Thomas Levesque Oct 31 '12 at 13:42
  • i have an array byte: byte[] b6 = new byte[23] {0x02,0x01,0x12,0x77,0x04,0xAA,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88 ,0x99 ,0xAA ,0xBB,0xCC ,0xDD ,0xEE,0xFF,0xCA,0x03}. The bytes of this array will be put in input to a serialport connection, and it's the command that this serial port register for the activity: write. So i need this 0x. – user1788654 Oct 31 '12 at 13:45
1

Try

int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);

for every splitted element of the string and add it to a List.

List<Byte> bytes = new List<Byte>();
foreach (var splittedValue in hexString.Split(' ')) {
    bytes.Add(int.Parse(splittedValue, System.Globalization.NumberStyles.HexNumber));
} 
return bytes.ToArray();
Benjamin
  • 120
  • 1
  • 7
0

You can use byte.Parse:

byte[] bytes = str.Split().Select(s => byte.Parse(s, NumberStyles.HexNumber)).ToArray();

To display bytes in hexadecimal representation use ToString override:

foreach (var b in bytes)
{
    Console.WriteLine("0x{0:X}", b);
    //or Console.WriteLine("0x" + b.ToString("X"));
}

You also can use "X" format and its modifications in string.Format.

tukaef
  • 9,074
  • 4
  • 29
  • 45
  • `byte.Parse` parses the string as decimal by default, not hexadecimal, so this won't work – Thomas Levesque Oct 31 '12 at 13:26
  • @2kay thank you for the representation, but i don't get the 0xAA, but only AA. I was trying to ask you how to get the 0xAA format. So i print 0xAA not only AA, because after i have to get this value in input to another procedure. – user1788654 Oct 31 '12 at 13:44
  • @user1788654, edited. Seems like you can get leading "0x" only by concatenating "0x" and hex representation of byte :( – tukaef Oct 31 '12 at 13:47
-3

you could use tostring function

Bahram
  • 1,464
  • 2
  • 22
  • 38