public class Test {
public static void main(String[] args) {
/*ByteBuffer bB = ByteBuffer.allocate(4);
int i = 10;
bB.putInt(i);

byte[] b = bB.array();

System.out.println(Arrays.toString(b));*/
int tmp = Integer.MAX_VALUE - 2134345;
System.out.println(Integer.toHexString(tmp));
byte[] b = new byte[4];
for(int j=0; j<b.length; j++) {
b[j] = (byte)(tmp & 0xFF);
System.out.println(Integer.toHexString(b[j]));
tmp = tmp>>8;
}

}
}结果为什么是这个:
7fdf6eb6
ffffffb6
6e
ffffffdf
7f我觉得第二行和第四行应该输入 b6和df的???

解决方案 »

  1.   


    public class Test {
        public static void main(String[] args) {
            /*ByteBuffer bB = ByteBuffer.allocate(4);
            int i = 10;
            bB.putInt(i);
            
            byte[] b = bB.array();
            
            System.out.println(Arrays.toString(b));*/
            int tmp = Integer.MAX_VALUE - 2134345;
            System.out.println(Integer.toHexString(tmp));
            byte[] b = new byte[4];
            //byte的取值范围是-128~127,第二行的值182,是第三行的值是110,第四行的值是223,第五行的值是127
            //所以b[0]=-74,b[1]=110,b[2]=-33,b[3]=127
           //然后调用Integer.toHexString()方法的时候,如果参数为负,那么无符号整数值为参数加上2的32次方;否则等于该参数。
            //所以就出现了那样的结果
            for(int j=0; j<b.length; j++) {
                b[j] = (byte)(tmp & 0xFF);
                System.out.println(Integer.toHexString(b[j]));
                tmp = tmp>>8;
            }
            System.out.println(Integer.toHexString(-33));
        }
    }
      

  2.   

    这个更清楚些,楼主可以看看!public class Test {
        public static void main(String[] args) {
            /*ByteBuffer bB = ByteBuffer.allocate(4);
            int i = 10;
            bB.putInt(i);
            
            byte[] b = bB.array();
            
            System.out.println(Arrays.toString(b));*/
            int tmp = Integer.MAX_VALUE - 2134345;
            System.out.println(Integer.toHexString(tmp));
            byte[] b = new byte[4];
            for(int j=0; j<b.length; j++) {
             //这儿打印每次tmp和0xFF相与的结果,第一次为182,第二次为110,第三行为223,第四行为127
             System.out.println(tmp & 0xFF);
             //所以下面强制转化以后(用补码表示),b[0]=-74,b[1]=110,b[2]=-33,b[3]=127
                b[j] = (byte)(tmp & 0xFF);
                //Integer.toHexString()方法,如果参数为负,那么无符号整数值为参数加上2的32次方;否则等于该参数。
                System.out.println(Integer.toHexString(b[j]));
                tmp = tmp>>8;
            }
            System.out.println(Integer.toHexString(-33));
        }
    }
      

  3.   

    明白了Integer.toHexString(int i)方法,如果传进去的是byte类型的会自动转换为int类型的,负数位扩展的时候,前面补1,I see 呵呵
      

  4.   

    public class Test {
        public static void main(String[] args) {
            /*ByteBuffer bB = ByteBuffer.allocate(4);
            int i = 10;
            bB.putInt(i);
            
            byte[] b = bB.array();
            
            System.out.println(Arrays.toString(b));*/
            int tmp = Integer.MAX_VALUE - 2134345;
            System.out.println(Integer.toHexString(tmp));
            byte[] b = new byte[4];
            for(int j=0; j<b.length; j++) {
                b[j] = (byte)(tmp & 0xFF);
                System.out.println(Integer.toHexString(b[j] & 0xFF));
                tmp = tmp>>8;
            }
            
        }
    }这才是正解