Created
January 21, 2014 07:49
-
-
Save xuechong87/8535893 to your computer and use it in GitHub Desktop.
byte to unsign byte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//尽管read()方法读取的是byte数据但是却返回int型,如果你想将这些存为原始的byte类型,你 | |
//可以把int转换成byte,例如: | |
byte[] b = new byte[10]; | |
for (int i = 0; i < b.length; i++) { | |
b[i] = (byte) System.in.read( ); | |
} | |
// 当然这个过程产生的是有符号的byte而不是read()方法返回的无符号byte(也就是说,一个取值 | |
//范围在-128到127之间的byte而不是0-255).一旦你搞清楚这些,那么在你的代码里无论处理的是有符号 | |
//还是无符号byte你都不会有问题.有符号的byte可以用如下的代码转换成0-255的int型: | |
int i = (b >= 0) ? b : 256 + b; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment