Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Bytes to floats / ints

I have a byte array sent via UDP from x-plane. The bytes (4) are all floats or integers… I tried to cast them to floats but no luck so far…

Example array: byte data[41] = {-66,30,73,0};

How do I convert 4 bytes into int or float and doesn't float use 8 bytes?

like image 628
JNK Avatar asked Dec 22 '10 20:12

JNK


People also ask

Can we convert byte to int in Java?

The intValue() method of Byte class is a built in method in Java which is used to return the value of this Byte object as int.

Can we convert int to float in Java?

So, when you assign an int value to float variable, the conversion of int to float automatically happens in Java. This is also called widening casting or widening primitive conversion. So, to convert an in to float using widening casting, you can just assign a value of int to float.

Are floats and ints the same size?

6 Answers. Show activity on this post. They are totally different - typically int is just a straightforward 2's complement signed integer, while float is a single precision floating point representation with 23 bits of mantissa, 8 bits exponent and 1 bit sign (see http://en.wikipedia.org/wiki/IEEE_754-2008).

Can we assign byte to int?

A bytes object can be converted to an integer value easily using Python. Python provides us various in-built methds like from_bytes() as well as classes to carry out this interconversion.


2 Answers

Note: I recommend @Ophidian's ByteBuffer approach below, it's much cleaner than this. However this answer can be helpful in understanding the bit arithmetic going on.

I don't know the endianness of your data. You basically need to get the bytes into an int type depending on the order of the bytes, e.g.:

int asInt = (bytes[0] & 0xFF) 
            | ((bytes[1] & 0xFF) << 8) 
            | ((bytes[2] & 0xFF) << 16) 
            | ((bytes[3] & 0xFF) << 24);

Then you can transform to a float using this:

float asFloat = Float.intBitsToFloat(asInt);

This is basically what DataInputStream does under the covers, but it assumes your bytes are in a certain order.

Edit - On Bitwise OR

The OP asked for clarification on what bitwise OR does in this case. While this is a larger topic that might be better researched independently, I'll give a quick brief. Or (|) is a bitwise operator whose result is the set of bits by individually or-ing each bit from the two operands.

E.g. (in binary)

   10100000
|  10001100
-----------
   10101100

When I suggest using it above, it involves shifting each byte into a unique position in the int. So if you had the bytes {0x01, 0x02, 0x03, 0x04}, which in binary is {00000001, 00000010, 00000011, 00000100}, you have this:

                                  0000 0001   (1)
                        0000 0010             (2 <<  8)
              0000 0011                       (3 << 16)
  | 0000 0100                                 (4 << 24)
  --------------------------------------------------------
    0000 0100 0000 0011 0000 0010 0000 0001   (67 305 985)

When you OR two numbers together and you know that no two corresponding bits are set in both (as is the case here), bitwise OR is the same as addition.

See Also

  • Wikipedia: Bitwise OR
like image 192
Mark Peters Avatar answered Oct 07 '22 23:10

Mark Peters


You probably want to make use of java.nio.ByteBuffer. It has a lot of handy methods for pulling different types out of a byte array and should also handle most issues of endianness for you (including switching the byte order if necessary).

byte[] data = new byte[36]; 
//... populate byte array...

ByteBuffer buffer = ByteBuffer.wrap(data);

int first = buffer.getInt();
float second = buffer.getFloat();

It also has fancy features for converting your byte array to an int array (via an IntBuffer from the asIntBuffer() method) or float array (via a FloatBuffer from the asFloatBuffer() method) if you know that the input is really all of one type.

like image 42
Ophidian Avatar answered Oct 07 '22 23:10

Ophidian