简体   繁体   中英

Retrieving 16-bit big endian value from byte array?

I have an unsigned 16-bit integer in big endian byte order: 0x01f1 but they are in a byte array as follows:

    Array
  [0]  0x01
  [1]  0xf1 

How do I "join" Array[0] with Array[1] such that I could have my value of 0x01f1 back?

You can use shift and bitwise operators to combine the two array elements as follows:

byte[] array = new byte[] { 0x01, 0xF1 };

int result = (array[0] << 8) | array[1];
// result == 0x01F1

use left shift operator as follows:

int myNum = a[0];
myNum = (myNum<<8);
myNum = myNum | a[1];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM