简体   繁体   中英

Byte array greater than 255

I get to be a tool bluetooth number of 16-bit and I need to convert it to a string. I am able to obtain a number up to 255 in this way:

byte[] readBuf = (byte[]) msg.obj;
String strIncom = new String(readBuf);                              
char c = strIncom.charAt(0);

definitely receive up to 255 because "byte" it is 8-bit. I tried to replace bytes with short but I get this error:

byte[] cannot be cast to short[]

try:

  ByteBuffer input=ByteBuffer.wrap((byte[])msg.obj);
  short my16bitValue=input.getShort();

read about byte order

I'm not sure, whether that is what you want to do...

(you can get a String from that using Short.toString(my16bitValue) ...)

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