Long story short, I'm reading some integer values from one file, then I need to store them in a byte array, for later writing to another file.
For example:
int number = 204;
Byte test = new Byte(Integer.toString(number));
This code throws:
java.lang.NumberFormatException: Value out of range. Value:"204" Radix:10
The problem here is that a byte can only store from -127 - 128, so obviously that number is far too high. What I need to do is have the number signed, which is the value -52, which will fit into the byte. However, I'm unsure how to accomplish this.
Can anyone advise?
Thanks
A much simpler approach is to cast it:
int number = 204;
byte b = (byte)number;
System.out.println(b);
Prints -52
.
你可以施展它:
Byte test = (byte) number;
用这个:
byte b = (byte) ((0xFF) & number);
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.