I need to translate an integer into 2 bytes (0 x...) How can I do this?
int port = 7777;
byte[] bufferPost = { 0xBC, 0x5F, ..., 0xbyte1OfIntValue, 0xbyte2OfIntValue };
I need to translate an integer into 2 bytes (0 x...) How can I do this?
int port = 7777;
byte[] bufferPost = { 0xBC, 0x5F, ..., 0xbyte1OfIntValue, 0xbyte2OfIntValue };
Something like this:
byte[] bufferPost = new byte[] {
0x12, 0x23, 0x45};
int port = 7777;
Array.Resize(ref bufferPost, bufferPost.Length + 2);
bufferPost[bufferPost.Length - 2] = (byte)(port & 0xFF);
bufferPost[bufferPost.Length - 1] = (byte)((port >> 8) & 0xFF);
// Let's have a look what's going on
Console.Write(string.Join(" ", bufferPost.Select(item => "0x" + item.ToString("x2"))));
Outcome:
0x12 0x23 0x45 0x61 0x1e