4

对于我的项目,我必须将整数转换为字节数组,然后将字节数组转换回整数。我做了下面的代码,但它不工作

    int temp=200;

    byte[] tempbyte= new byte[4];

String s= String.valueOf(temp);
tempbyte= s.getBytes(); // integer to byte array conversion

String s2 = String.valueOf(ByteBuffer.wrap(tempbyte).getInt());

int temp2 = Integer.parseInt(s2); // byte array to integer conversion

System.out.println("the value of temp2 = "+temp2);
4

4 回答 4

2

尝试这样的事情: -

byte[] arr = { 0x00, 0x01 };
ByteBuffer wrapped = ByteBuffer.wrap(arr);
short num = wrapped.getShort(); 

ByteBuffer dbuf = ByteBuffer.allocate(2);
dbuf.putShort(num);
byte[] bytes = dbuf.array();

使用java.nio命名空间的类

于 2012-11-18T07:22:45.863 回答
1

这些是我多年前所做的可以帮助您的功能:

public static float byteArrayToFloat(byte[] p_bArray)
{
    int nValue;

    nValue = (0xFF & p_bArray[0]);
    nValue = nValue | ((0xFF & p_bArray[1]) << 8);
    nValue = nValue | ((0xFF & p_bArray[2]) << 16);
    nValue = nValue | ((0xFF & p_bArray[3]) << 24);

    float fValue = Float.intBitsToFloat(nValue);

    return fValue;
}

public static byte[] floatToByteArray(float p_fValue)
{
    byte [] bArray = new byte[4];
    int nValue;

    nValue = Float.floatToIntBits(p_fValue);

    bArray[0] = (byte)nValue;
    bArray[1] = (byte)(nValue >> 8);
    bArray[2] = (byte)(nValue >> 16);
    bArray[3] = (byte)(nValue >> 24);

    return bArray;
}

public static double byteArrayToDouble(byte[] p_bArray)
{
    long lValue;

    lValue = (long)p_bArray[0] & 0xFF;
    lValue = lValue | (((long)p_bArray[1] & 0xFF) << 8);
    lValue = lValue | (((long)p_bArray[2] & 0xFF) << 16);
    lValue = lValue | (((long)p_bArray[3] & 0xFF) << 24);
    lValue = lValue | (((long)p_bArray[4] & 0xFF) << 32);
    lValue = lValue | (((long)p_bArray[5] & 0xFF) << 40);
    lValue = lValue | (((long)p_bArray[6] & 0xFF) << 48);
    lValue = lValue | (((long)p_bArray[7] & 0xFF) << 56);

    double dValue = Double.longBitsToDouble(lValue);

    return dValue;
}

public static byte[] doubleToByteArray(double p_dValue)
{
    byte [] bArray = new byte[8];
    long lValue;

    lValue = Double.doubleToLongBits(p_dValue);

    bArray[0] = (byte)lValue;
    bArray[1] = (byte)(lValue >> 8);
    bArray[2] = (byte)(lValue >> 16);
    bArray[3] = (byte)(lValue >> 24);
    bArray[4] = (byte)(lValue >> 32);
    bArray[5] = (byte)(lValue >> 40);
    bArray[6] = (byte)(lValue >> 48);
    bArray[7] = (byte)(lValue >> 56);

    return bArray;
}

public static int byteArrayToInt(byte[] p_bArray)
{
    int nValue;

    nValue = ((int)p_bArray[0]) & 0x000000FF;
    nValue = nValue | (((int)p_bArray[1] & 0x000000FF) << 8);
    nValue = nValue | (((int)p_bArray[2] & 0x000000FF) << 16);
    nValue = nValue | (((int)p_bArray[3] & 0x000000FF) << 24);

    return nValue;
}

public static byte[] intToByteArray(int p_nValue)
{
    byte [] bArray = new byte[4];

    bArray[0] = (byte)p_nValue;
    bArray[1] = (byte)(p_nValue >> 8);
    bArray[2] = (byte)(p_nValue >> 16);
    bArray[3] = (byte)(p_nValue >> 24);

    return bArray;
}
于 2012-11-18T07:23:40.077 回答
0

我的版本:

    int i = 1;
    byte[] bytes = BigInteger.valueOf(i).toByteArray();
    i = new BigInteger(bytes).intValue();
于 2012-11-18T07:34:00.487 回答
0

Integer 类具有将数字转换为字节表示的方法。

    String i = Integer.toBinaryString(4);

你也可以反之亦然

    String binaryString = "100";
    int base = 2;
    int decimal = Integer.parseInt(binaryString, base);
于 2012-11-18T07:37:08.163 回答