2

Lets assume that I have a hex string of

00 00 04 01 11 00 08 00 06 C2 C1 BC

With this the 7th, 8th, and 9th octet are a number I need to generate. The hex is

00 06 C2

This number turns out to be 1730. With the following, how can I simplify this?

byte b1 = 0x00;
byte b2 = 0x06;
byte b3 = 0xC2;

Console.WriteLine(Convert.ToInt32((Convert.ToString(b1, 16)) + (Convert.ToString(b2, 16)) + (Convert.ToString(b3, 16)), 16));

I know there has to be a simpler way. I tried Console.WriteLine((b1 + b2 + b3).ToString()); but it doesn't work.

Gabriel Graves
  • 1,751
  • 1
  • 22
  • 40

3 Answers3

8

Try:

int result = b3 | (b2 << 8) | (b1 << 16);

Assuming that b1, b2 and b3 are the byte values that you need to convert.

The << operator shifts its operand left by the specified number of bits.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • This worked perfectly! I will mark this as the answer as soon as it lets me! – Gabriel Graves May 16 '13 at 06:40
  • 3
    As a small note: when dealing with `byte` arithmetic, `|` would be preferable to `+`; this may be important when "negative" numbers are involved (where the MSB is set) – Marc Gravell May 16 '13 at 06:41
  • (the OP put the `+` => `|` through as a suggested edit... feel free to change it back, but I maintain `|` improves the answer) – Marc Gravell May 16 '13 at 06:47
  • I was just changing the + to a | but it required me to change 6 characters to make an edit so I added a comment at the end. – Gabriel Graves May 16 '13 at 06:55
  • @MarcGravell True dat - I wasn't too concerned since it was only 3 bytes (and so `+` would always work), but it's best to do it the right way. – Matthew Watson May 16 '13 at 07:44
  • @MarcGravell Actually in this case it is always safe to use `+` because the bits in the bytes don't overlap - but it's better to use `|` because that will always work even in cases when the bits do overlap. Best practice and all that. – Matthew Watson May 16 '13 at 08:26
2

You can use the BitConverter class to convert an array of bytes to an int.

// Add the bytes to an array, starting with b3, then b2, b1 and 0 to
// make it 4 bytes in total.
byte[] b = new byte[] { 0xC2, 0x06, 0x00, 0x00 };
int i = BitConverter.ToInt32(b, 0);

i will now have the value 1730.

John Willemse
  • 6,608
  • 7
  • 31
  • 45
  • 1
    That's a pretty hard way of doing something fairly simple, though - and it opens all sorts of questions about "endianness" etc. I'll work, but to be done properly you'd need to check `BitConverter.IsLittleEndian`, etc – Marc Gravell May 16 '13 at 06:45
  • 1
    Yes, I realize that, but didn't want to make it more complicated than it already was. I prefer the bitshifting way, but wanted to propose an alternative to the other answers. – John Willemse May 16 '13 at 06:46
1

You can try this:

    byte b1 = (byte)0x00;
    byte b2 = (byte)0x06;
    byte b3 = (byte)0xC2;
    int i = ((b1 & 0xFF) << 16) | ((b2 & 0xFF) << 8) | (b3 & 0xFF);

EDIT:

    byte b1 = (byte)0x00;
    byte b2 = (byte)0x06;
    byte b3 = (byte)0xC2;
    int i = (b1 << 16) | (b2 << 8) | b3;
Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
  • 1
    I saw that sneaky edit ;) Note: there's no benefit doing an `& 0xFF` if you know the source is `byte` - there are no other bits that can be set – Marc Gravell May 16 '13 at 06:43