|
||
| Reading data from a stream Cast the int to a byte |
||
If you want to store the raw bytes instead, you can cast the int to a byte.
byte[] b = new byte[10]; for (int i = 0; i < b.length; i++) { b[i] = (byte) System.in.read(); }
Of course, this produces a signed byte instead of the unsigned byte returned by the read() method (that is, a
byte in the range -128 to 127 instead of 0 to 255). However as long as you're clear in your mind and your code about which one you're
working with, this doesn't present a problem.
Signed bytes can be converted back to ints in the range 0 to 255 like this:
int i = b >= 0 ? b : 256 + b;
|
||
|
|
||
