Skip to content

Instantly share code, notes, and snippets.

@xuechong87
Created January 21, 2014 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xuechong87/8535893 to your computer and use it in GitHub Desktop.
Save xuechong87/8535893 to your computer and use it in GitHub Desktop.
byte to unsign byte
//尽管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