情况是这样的,有个字节数组如:
Byte tmpByte[]={88,129,89,1};
现要将这个字节tmpByte数组的4个字节转换为32位的有符号的整型,
结果是:22643032
请问,这个是怎么转过来的 ,java里面有没有现成的函数可用呢

解决方案 »

  1.   

    byte[] tmpByte = {88,129,89,1};
    上面搞错了, 纠正;是这样的
      

  2.   

    低位前置结构
    {88,129,89,1} =>
    十六进制{01,59,89,58} =>
    0x01598958 =>
    22643032tmpByte[3] << 24 + tmpByte[2] << 16 + tmpByte[1] << 8 + tmpByte[0]
      

  3.   


    int s=0;
    for(int i=0;i<tmpByte.length;i++){
      s<<=8;
      s|=(tmpByte[i] & 0x000000ff);
    }
    return s;
      

  4.   

    下面是 aspx里面的 一段代码:如何用java来改写?
            byte[] byteArray = { 0x00, 0x00, 0x0e, 0x89, 0x00, 0x00, 0x27, 0xa7 };
            // write bigid
            byte[] intByte = System.BitConverter.GetBytes(bigid);
            byteArray[48] = intByte[3];
            byteArray[49] = intByte[2];
            byteArray[50] = intByte[1];
            byteArray[51] = intByte[0];
            // write  smid
            intByte = System.BitConverter.GetBytes(smid);
            byteArray[52] = intByte[3];
            byteArray[53] = intByte[2];
            byteArray[54] = intByte[1];
            byteArray[55] = intByte[0];
      

  5.   

     下面这段也是aspx代码:如何转换为java代码
           Stream dataStream ;
            BinaryReader BR = new BinaryReader(dataStream);
            // skip 3 byte
            BR.ReadByte();
            BR.ReadByte();
            BR.ReadByte();
            if (0 == BR.ReadInt32())
            {
                byte[] tmpByte = new byte[4];
                tmpByte[3] = BR.ReadByte();
                tmpByte[2] = BR.ReadByte();
                tmpByte[1] = BR.ReadByte();
                tmpByte[0] = BR.ReadByte();
                int tiia = BitConverter.ToInt32(tmpByte, 0);
                out double tA = (double)tiia / 1000000D;
            }
      

  6.   

    把上面2段重新整理下:
    byte[] byteArray = { 0x00, 0x00, 0x0e, 0x89, 0x00, 0x00, 0x27, 0xa7 };
      // write bigid
      byte[] intByte = System.BitConverter.GetBytes(bigid);
      byteArray[0] = intByte[3];
      byteArray[1] = intByte[2];
      byteArray[2] = intByte[1];
      byteArray[3] = intByte[0];
      // write smid
      intByte = System.BitConverter.GetBytes(smid);
      byteArray[4] = intByte[3];
      byteArray[5] = intByte[2];
      byteArray[6] = intByte[1];
      byteArray[7] = intByte[0];
    /////////////////////////////////////////////////
      Stream dataStream =...;
      BinaryReader BR = new BinaryReader(dataStream);
      // skip 3 byte
      BR.ReadByte();
      BR.ReadByte();
      BR.ReadByte();
      if (0 == BR.ReadInt32())
      {
      byte[] tmpByte = new byte[4];
      tmpByte[3] = BR.ReadByte();
      tmpByte[2] = BR.ReadByte();
      tmpByte[1] = BR.ReadByte();
      tmpByte[0] = BR.ReadByte();
      int tiia = BitConverter.ToInt32(tmpByte, 0);
      out double tA = (double)tiia / 1000000D;
      }
      
      

  7.   

    为什么要右移八位以及&0xff了?