C
C
Chvalov2015-10-29 11:02:41
Java
Chvalov, 2015-10-29 11:02:41

Float back to bytes how to implement?

The code with which I get the value from the byte float:

byte[] response = new byte[]{-39, 118, -98, 60};

         // Получаем коефециент
        int asInt = (response[0] & 0xFF)
                | ((response[1] & 0xFF) << 8)
                | ((response[2] & 0xFF) << 16)
                | ((response[3] & 0xFF) << 24);

        float asFloat = Float.intBitsToFloat(asInt);
At the output I get: The 0.01934378
question is, how can I write Float back to the byte array if the asInt data is not in my class?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Chvalov, 2015-10-29
@Chvalov

Tried water like this:

byte[] b = ByteBuffer.allocate(4).putFloat(asFloat).array();
But at the output I get in reverse order [60, -98, 118, -39]
UPD : I did the water like this:
byte[] gf = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putFloat(asFloat).array();

        System.out.println("Значение обратно" + Arrays.toString(gf));
And everything works fine!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question