我做了以下的尝试,结果不对。
private void byte2int(byte[] byteValue, int intValue)
{
try
{
PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pos);
pos.write(byteValue, 0, 4);
DataInputStream dis = new DataInputStream(pis);
intValue = dis.readInt();
}
catch(IOException e)
{
System.out.println("sdfasdfsdafds");
}
}
谁有好的方法呢,共享一下,谢谢。

解决方案 »

  1.   

    楼主!!!方法的结构就不对, 基本数据类型不能按引用传递的,所以你的int型结果无法传出。
      

  2.   

    不知用下面的方法能否得到楼主所要得到的结果Integer.parseInt(new String(byteValue))
      

  3.   

    跟上面的一样,只是写法不同byte[] byteValue = ...
    Integer.parseInt(byteValue.toString())
      

  4.   

    注意是否BigEndian    public static int makeInt(byte b3, byte b2, byte b1, byte b0) {
            return (int) ((((b3 & 0xff) << 24) | ((b2 & 0xff) << 16)
                    | ((b1 & 0xff) << 8) | ((b0 & 0xff) << 0)));
        }
      

  5.   

    byte[] oByteArray = {1,2,3,4};
    int iIntValue = 0;
    for(int i=0;i<4;i++)
    {
    iIntValue = iIntValue<<8;
    iIntValue = iIntValue + (int)oByteArray[i];
    }
    或者
    System.out.println(oByteArray[3] + oByteArray[2]* 256 + oByteArray[1]*256*256 + oByteArray[0]*256*256*256);
    ^_^
      

  6.   

    难道要这样:
    public int decodeInt(byte[] temp)
        {
            int result = 0;        for (int i = 0; i < temp.length; ++i) //取数据
            {
                result = (int) (((byte)result) << 8);
                result += (int) (temp[ i ] & 0x000000ff);
            }        return result;
        }
      

  7.   

    分析了一下熊猫的方法
    s = String(byteValue)会使byteValue的数组值变为\u0xx,并不是数字字符,
    而parseInt(String)是把"0~9"变为int的,所以会有异常
      

  8.   

    private void byte2int(byte byteValue, int intValue)
        {
      
         System.out.println(String.valueOf((int)byteValue));
        
        }
      

  9.   

    谢谢 homesos(熊猫贩子) 指出了我的错误
    谢谢 yonghar(ohno) 我采用了你的方法
    谢谢 xhhsld(bluepluto) 你的方法也对