4

For my project I have to convert an integer into byte array and byte array back to integer. I did the below code but it is not working

    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 Answers 4

2

Try something like this:-

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();

Use the classes of java.nio namespace

Sign up to request clarification or add additional context in comments.

Comments

1

Theses are functions I've made years ago that could help you :

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;
}

2 Comments

thank you @alexandre. have created a new class with all these methods.
You are welcome, I'm happy that my old developments could help someone! Don't forget to mark as accepted!
0

My version:

    int i = 1;
    byte[] bytes = BigInteger.valueOf(i).toByteArray();
    i = new BigInteger(bytes).intValue();

Comments

0

Integer class has a method to convert number into byte representation .

    String i = Integer.toBinaryString(4);

and you can do vice-versa as well

    String binaryString = "100";
    int base = 2;
    int decimal = Integer.parseInt(binaryString, base);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.