import java.io.*;
public class ByteArrayTester
{
public static void main(String args[])throws IOException
{
byte[] buff=new byte[]{2,15,67,-1,-9,9};
ByteArrayInputStream in=new ByteArrayInputStream(buff,1,4);
int data=in.read();
while(data!=-1)
{
System.out.println(data+" ");
data=in.read();
}
in.close();
}
}
结果输出:15 67 255 247 
对于 byte类型的15 二进制的形式为 00001111 转为int 类型是为 00000000 00000000 0000000 00001111 因此结果依然是15,但是当是-1时转化完的结果是255
同理其他的都一样。可是在进行基本类型转化的时候却不是这样;
byte b1=15,b2=-1,b3=-9;
int a1=b1,a2=b2,a3=b3;
此时a1,a2,a3的值是15,-1,-9
这是为什么呀  不明白大家遇没遇到这个问题
希望高手给讲解一下

解决方案 »

  1.   

    public int read()
    从此输入流中读取下一个数据字节。
    返回一个 0 到 255 范围内的 int 字节值。
    如果因为到达流末尾而没有可用的字节,则返回值 -1。 返回的是0-255,所以是负数的话就会溢出,返回一个正数
    原数加上(减去)256的倍数,使得结果在0-255中byte到int转型是扩充范围,不会引起数值的变化
      

  2.   

    public synchronized int read() {
    return (pos < count) ? (buf[pos++] & 0xff) : -1;
        }