public static byte[] intTobyte(int number) 
{
int temp = number;
byte[] b=new byte[4];
for (int i=b.length-1;i>-1;i--)
{
b[i] = new Integer(temp&0xff).byteValue();//temp&0xff 是什么意思
temp = temp >> 8;  //为什么?
}
return b;
}
       请解释一下

解决方案 »

  1.   

    temp&0xff 与运算取最后一个字节的内容
    temp = temp >> 8; 一个字节是八位 取第二个字节必须向右移八位
      

  2.   

    package com.fei.test;public class Test
    {
    public static byte[] intTobyte(int number)
    {
    int temp = number;
    byte[] b = new byte[4];
    System.out.println("the length of int:"+Integer.SIZE);
    System.out.println("the byte of byte:"+Byte.SIZE);
    System.out.println("this is the length of array b:"+b.length);
    for (int i = b.length - 1; i > -1; i--)
    {
    System.out.println("this is the binary of temp:"+Integer.toBinaryString(temp));
    b[i] = new Integer(temp & 0xff).byteValue();// temp&0xff 就是将保留temp的低八位
    temp = temp >> 8; // 右移八位,下次取的temp的9-16位上的数据
    }
    return b;
    } public static void main(String[] args)
    {
    byte[] result = Test.intTobyte(99999);


    }
    }
      

  3.   

    程序功能:int型转换byte数组
    解释:
    1.int在java中占四个字节,所以必须一个一个取出
    2.符号“&”是“与”,0xff代表16进制数15,即8位全为1。与temp“与”后,得到temp低8位值,赋给b[i],即得到一个字节;
    3.符号“>>”是“左移”,temp = temp >> 8;表示temp左移8位再赋给temp,继续循环;
    明白了吗?
    今天愚人节,但没忽悠你哈