Convert a byte array to a float

I

Ignaz Kraehenmann

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.
 
I

Ignaz Kraehenmann

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.
 
X

xarax

Ignaz Kraehenmann said:
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];
 
L

Lee Fesperman

xarax said:
Ignaz Kraehenmann said:
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);
 
A

Anthony Borla

Ignaz Kraehenmann said:
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;
}
}

// ----------------------------------------------------------
 
J

Jim

Lee said:
xarax said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top