0

I am building a client application which needs to send packet to server in a particular format such that if the byte is 0xA3, the server expects it as {0x3A, 0x33}

I had used the below approach earlier. It works well if the byte is for instance 0x89. But if the byte is 0xA3 it doesnt work

string hex = hexStr .Length == 1 ? "0" + hexStr:hexStr ;
byte packet1 = (byte)(int.Parse(hex[0].ToString(), System.Globalization.NumberStyles.HexNumber) + 0x30);
byte packet2 = (byte)(int.Parse(hex[1].ToString(), System.Globalization.NumberStyles.HexNumber) + 0x30);

Examples of expected output

  1. input => 0x89 , Output => {0x38, 0x39}
  2. input => 0xA3 , Output => {0x3A, 0x33}

However if i use the above code i get the following output

  1. input => 0x89 , Output => {0x38, 0x39}
  2. input => 0xA3 , Output => {0x41, 0x33}
6
  • Does it actually always start with "0x"? If so, surely you can ditch your first part. If not, you really need to tell us what the cases are. Now, you seem to be trying to obtain two bytes out of this data - but two hex digits represents a single byte. Just use byte.Parse... and if it doesn't work, please tell us which cases it doesn't work for... Commented May 5, 2016 at 5:32
  • @JonSkeet I have edited my question. The requirement is that i should form a byte array from the hexStr such that each nibble is represented as a separate byte in the array Commented May 5, 2016 at 5:47
  • 1
    Wow, that wasn't at all obvious before... it's still not clear why there's an addition of 0x30 in each case... why aren't your nibbles 0xA, 0x6? You still haven't given us an example of what doesn't work, either. Commented May 5, 2016 at 5:48
  • @JonSkeet. I mixed up the question earlier..sorry for that.. i am a building client application which needs to send packet to server in that particular format. So if the byte is 0xA6, the server expects it as {0x3A, 0x36} Commented May 5, 2016 at 5:52
  • Okay, so you should explain that in the question - and you still haven't given an example which doesn't work. Commented May 5, 2016 at 5:55

1 Answer 1

1

The problem isn't in the code you've shown.

You need to do all your math in hexadecimal, and convert to strings (if needed) into hexadecimal as well:

string hex = "A3";
byte packet1 = (byte)(int.Parse(hex[0].ToString(), NumberStyles.HexNumber) + 0x30);
byte packet2 = (byte)(int.Parse(hex[1].ToString(), NumberStyles.HexNumber) + 0x30);

Console.WriteLine("{0:X2}, {1:X2}", packet1, packet2); // 3A, 33

works exactly as you expect.

The results you got seem to indicate that you took 30 (decimal), added 0xA (11), and printed as decimal, rather than hexa-decimal. This does not happen in the code you've posted, so just fix your actual code and you'll be fine.

Sign up to request clarification or add additional context in comments.

1 Comment

Seems like it got messed up after that ..thanks for the response

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.