Reading byte [] as unsigned short Java

I need to read a 16-bit byte array as an unsigned short number, and Java does not support unsigned short type.
So how can I do this ?? Please, help!

+7
source share
3 answers

Assuming it is available as binary data from a non-Java source that you should read and work with values ​​in Java: Read it as a (signed) short , and then convert it to int as follows:

 int intVal = shortVal >= 0 ? shortVal : 0x10000 + shortVal 

You cannot represent all unsigned short values ​​in short, but in int, you can.

+9
source

I do not know whether to use unsigned short, but if you do not, you can always use char to store unsigned short. Take a look at this article, which gives a simple overview.

http://darksleep.com/player/JavaAndUnsignedTypes.html

+4
source
0
source

All Articles