Java – how to append two bytes to int
•
Java
I tried to append two bytes with hexadecimal values and store them as integers Obviously, everything is an unsigned value
Two bytes 0x20 0x07
Integer editor: Oh, I made a big mistake here Sorry for all the confusion I want to store integers 2007 instead of 0x2007 I'm really sorry
Is there any way to do this without converting bytes to string, appending and switching to int? Or is converting to string the only way?
Solution
You can try
byte b1 = (byte) 0x90; byte b2 = (byte) 0xF7; int i = ((b1 & 0xFF) << 8) | (b2 & 0xFF);
However, if you are using datainputstream or bytebuffers, you usually do not need to do so In both cases, you can use getshort
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码