问题还可以变一下:
byte[] b={12,14,15,16};
把b连成一个32位的;
然后按4位一取,换成16进制,输出?

解决方案 »

  1.   

    public class ByteTran {
    static byte[] b={12,14,15,16};

    public static void main(String[] args){
    Integer resut = 0;
    for(int i = 0; i < b.length; i++){
    resut |= (b[i]<<((b.length-i-1)*8));
    }
    //输出十六进制
    System.out.println(Integer.toHexString(resut));
    }
    }
      

  2.   

    一般int变成byte数组是反的。高位在后,低位在前。byte[] b={12,14,15,16};
    int convertBytesToInt(byte[] b)
    {
       return (b[3]<<24)|(b[2]<<16)|(b[1]<<8)|b[0];
    }
      

  3.   

    补充一下,由于java里面没有unsigned byte,所以需要通过&0xff将符号位去掉。int convertBytesToInt(byte[] b)
    {
    if(b == null || b.length != 4)
       return 0;   int n0 = b[3];
       n0= (n0&0xff)<<24;
       int n1 = b[2];
       n1= (n1&0xff)<<16;
       int n2 = b[1];
       n2= (n2&0xff)<<8;
      int n3 = b[0];
       n3= (n3&0xff)<<24;
       
       return n0|n1|n2|n3;
      

  4.   

    int convertBytesToInt(byte[] b)
    {
    if(b == null || b.length != 4)
       return 0;   int n0 = (b[3]&0xff)<<24;
       int n1 = (b[2]&0xff)<<16;
       int n2 = (b[1]&0xff)<<8;
       int n3 = (b[0]&0xff);
       
       return n0|n1|n2|n3;
    }