Convert a byte array to a float

Discussion in 'Java' started by Ignaz Kraehenmann, Dec 20, 2003.

  1. Coming from assembler, I'm stuck with some basic Java. Wether I
    interpret 4 bytes as an array of bytes, an integer, a string or anything
    is up to the programmer, however, Java does not seem to let you do
    that. How can I interpret 4 bytes (declared as an array of bytes) as a
    float?

    Thanks, Igi.
    Ignaz Kraehenmann, Dec 20, 2003
    #1
    1. Advertising

  2. Ignaz Kraehenmann

    ak Guest

    ak, Dec 20, 2003
    #2
    1. Advertising

  3. OK, then how do a get byte[] into an integer?
    256*256*256*byte[0] + 256*256*byte[1] + 256*byte[2] + byte[3] ??? (I
    hope there is another way...)

    Igi.
    Ignaz Kraehenmann, Dec 20, 2003
    #3
  4. Ignaz Kraehenmann

    xarax Guest

    "Ignaz Kraehenmann" <> wrote in message
    news:3fe46a99$...
    > OK, then how do a get byte[] into an integer?
    > 256*256*256*byte[0] + 256*256*byte[1] + 256*byte[2] + byte[3] ??? (I
    > hope there is another way...)
    >
    > Igi.


    int myInt = (byte[0] << 24) | (byte[1] << 16) | (byte[2] << 8) | byte[3];
    xarax, Dec 20, 2003
    #4
  5. xarax wrote:
    >
    > "Ignaz Kraehenmann" <> wrote in message
    > news:3fe46a99$..
    > > OK, then how do a get byte[] into an integer?
    > > 256*256*256*byte[0] + 256*256*byte[1] + 256*byte[2] + byte[3] ??? (I
    > > hope there is another way...)
    > >
    > > Igi.

    >
    > int myInt = (byte[0] << 24) | (byte[1] << 16) | (byte[2] << 8) | byte[3];


    byte is signed. You need to do this:

    int myInt = (byte[0] << 24) |
    ((byte[1] & 0xff) << 16) |
    ((byte[2] & 0xff) << 8) |
    (byte[3] & 0xff);

    .... or (for little-endian):

    int myInt = (byte[3] << 24) |
    ((byte[2] & 0xff) << 16) |
    ((byte[1] & 0xff) << 8) |
    (byte[0] & 0xff);

    --
    Lee Fesperman, FirstSQL, Inc. (http://www.firstsql.com)
    ==============================================================
    * The Ultimate DBMS is here!
    * FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
    Lee Fesperman, Dec 20, 2003
    #5
  6. "Ignaz Kraehenmann" <> wrote in message
    news:...
    >
    > Coming from assembler, I'm stuck with some basic Java.
    >


    You mean coming from an environment where there is no typing, with all
    [process addressable] memory being merely bytes to be interpreted in any way
    you wish. It's difficult to relinquish that freedom and power ;) !

    >
    > Whether I interpret 4 bytes as an array of bytes, an integer, a
    > string or anything is up to the programmer, however, Java does
    > not seem to let you do that. How can I interpret 4 bytes (declared
    > as an array of bytes) as a float?
    >


    Simply by digging into the facilities Java offers to perform such tasks,
    noting that these facilities are, sometimes, seemingly roundabout means of
    achieving said tasks.

    See code below.

    I hope this helps.

    Anthony Borla

    // Sample Code --------------------------------------------

    import java.io.ByteArrayInputStream;
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.io.EOFException;

    public class BitsToFloat
    {
    public static void main(String[] args)
    {
    byte[] b = new byte[] { 0x01, 0x02, 0x03, 0x04 };

    System.out.println("toFloat: " + toFloat(b));
    System.out.println("convertByteArrayToFloat: "
    + convertByteArrayToFloat(b));
    }

    // Quick, low-overhead conversion taking advantage of knowledge of
    // how Java effects these conversions
    public static float toFloat(byte[] b)
    {
    if (b.length != 4) return 0.0F;

    // Same as DataInputStream's 'readInt' method
    int i = (((b[0] & 0xff) << 24) | ((b[1] & 0xff) << 16) |
    ((b[2] & 0xff) << 8) | (b[3] & 0xff));

    // This converts bits as follows:
    /*
    int s = ((i >> 31) == 0) ? 1 : -1;
    int e = ((i >> 23) & 0xff);
    int m = (e == 0) ? (i & 0x7fffff) << 1 : (i & 0x7fffff) | 0x800000;
    */

    return Float.intBitsToFloat(i);
    }

    // Longer-winded routine - same technique useful for converting any
    // byte array values to other types in a type-safe way via streams
    public static float convertByteArrayToFloat(byte[] b)
    {
    if (b.length != 4) return 0.0F;

    DataInputStream dis = null; float f = 0.0F;

    try
    {
    dis = new DataInputStream(new ByteArrayInputStream(b));

    // Effectively performs:
    // * readInt call
    // * Float.intBitsToFloat call
    f = dis.readFloat();
    }

    catch (EOFException e)
    {
    // Handle ...
    }

    catch (IOException e)
    {
    // Handle ...
    }

    finally
    {
    if (dis != null) { try { dis.close(); } catch (IOException e) {} dis =
    null; }
    }

    return f;
    }
    }

    // ----------------------------------------------------------
    Anthony Borla, Dec 20, 2003
    #6
  7. Ignaz Kraehenmann

    Roedy Green Guest

    Roedy Green, Dec 21, 2003
    #7
  8. Thanks to everyone! It was really helpful.

    Igi.
    Ignaz Kraehenmann, Dec 21, 2003
    #8
  9. Ignaz Kraehenmann

    Jim Guest

    Lee Fesperman wrote:

    > xarax wrote:
    >
    >>"Ignaz Kraehenmann" <> wrote in message
    >>news:3fe46a99$..
    >>
    >>>OK, then how do a get byte[] into an integer?
    >>>256*256*256*byte[0] + 256*256*byte[1] + 256*byte[2] + byte[3] ??? (I
    >>>hope there is another way...)
    >>>
    >>>Igi.
    >>>

    >>int myInt = (byte[0] << 24) | (byte[1] << 16) | (byte[2] << 8) | byte[3];
    >>

    >
    > byte is signed. You need to do this:
    >
    > int myInt = (byte[0] << 24) |
    > ((byte[1] & 0xff) << 16) |
    > ((byte[2] & 0xff) << 8) |
    > (byte[3] & 0xff);
    >
    > ... or (for little-endian):
    >
    > int myInt = (byte[3] << 24) |
    > ((byte[2] & 0xff) << 16) |
    > ((byte[1] & 0xff) << 8) |
    > (byte[0] & 0xff);



    Or using 1.4

    import java.nio.ByteBuffer;

    public class testNIO
    {
    public static void main(String [] args)
    {
    byte [] data = new byte[] {1,2,3,4};

    ByteBuffer b = ByteBuffer.wrap(data);

    System.out.println(b.getInt());
    System.out.println(b.getFloat());

    } // main()
    }

    The ByteBuffer class is worth an evening playing
    around with.

    Jim
    Jim, Dec 21, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page