This discussion is archived
6 Replies Latest reply: Sep 6, 2013 5:03 PM by jschellSomeoneStoleMyAlias

# How to read  and convert single precision 32 bit float (IEEE754) from a stream

Currently Being Moderated

I am reading single precision 32 bit float (IEEE754) from a stream and I stored the values in a byte array. I want to convert that float value to everyday numbers so that I can do calculations (adding, finding out standard deviation, etc.). The numerical values are 10 to 11 digits long, so I am thinking to use "long" type. (Should I use bigInteger?). Anyway my problem is should I use getFloat(index) or getInt(index)? Both read 4 bytes, only the return values are either int or float. I will then have to convert the values into long, keeping in mind that it is IEEE754  single precision 32 bit float, in little endian byte order.

So far, through web search, I have cobbled up these:

[Code]

FileInputStream fis = new FileInputStream(file);

DataInputStream dis = new DataInputStream(fis);

byte[] bs = new byte[appropriate_size];

ByteBuffer b = ByteBuffer.wrap(bs).order(ByteOrder.LITTLE_ENDIAN);

for (int i = 0; i < bs.length; i+=4){

Float f = b.getFloat(i);

//convert that float value and save in an array

data[some_index] = converted_value;

}

[/Code]

Questions:

(0)  Should I use getFloat(index) or getInt(index)?

(1) Is the loop statement correct? My understanding is 4 bytes is a "single" piece of data I am interested in, the one I need to convert to a large decimal number ("long" type)

Suspicion: my loop is controlled by byte array (bs), but I am reading from byteBuffer (b)..... is it right?

(2) How do I convert the float value to appropriate decimal value and then convert to long?

I found "floatToIntBits(float)" method, which, as per Oracle API, "returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout..."

Isn't it what I want? I am asking to all of you as well as to myself.

I should have known better, but I am confused.

Reason: I used a hex editor to "see" what is going on in the float area. It showed hex strings, and the numerical value was the same as that of output from the floatToIntBits method.

(The hex editor has big/little endian toggle, and "little endian" was selected.) How could they have the same output? The i.Hex did not mention anything about IEEE754 32 bit single precision float.

Also, I found an online hex/IEEE754 float converter, and the output was different. hence the confusion.

The hex string sequences I saw in the hex editor is like 012A5F4E　B4F2454E　926A5F4E...

Dear gurus, what would be appropriate conversion of, let's say, the first one (012A5F4E)?

Please give some guidance. Thank you.

• ###### 1. Re: How to read  and convert single precision 32 bit float (IEEE754) from a stream
Currently Being Moderated

Hi,

BigInteger has has two relevant constructors:

BigInteger (byte[])

BigInteger (String, int)

but you could also use Integer (String, int)

Phil

• ###### 2. Re: How to read  and convert single precision 32 bit float (IEEE754) from a stream
Currently Being Moderated
I am reading single precision 32 bit float (IEEE754) from a stream and I stored the values in a byte array. I want to convert that float value to everyday numbers so that I can do calculations (adding, finding out standard deviation, etc.).

Float values ARE 'everyday' numbers. You can use them in calculations.

Why do you think they need to be converted so some other format? Java will convert numbers to other formats as necessary if you have a calculation that uses more than one format.

(0)  Should I use getFloat(index) or getInt(index)?

Your 4 bytes are a 'Float' so you need to use 'getFloat'.

(1) Is the loop statement correct? My understanding is 4 bytes is a "single" piece of data I am interested in, the one I need to convert to a large decimal number ("long" type)

Suspicion: my loop is controlled by byte array (bs), but I am reading from byteBuffer (b)..... is it right?

No - you should use the ByteBuffers 'limit'.

(2) How do I convert the float value to appropriate decimal value and then convert to long?

You don't need to convert the float; just use it in the calculation.

• ###### 3. Re: How to read  and convert single precision 32 bit float (IEEE754) from a stream
Currently Being Moderated

Hmm, I must have misunderstood the OP's question.

• ###### 4. Re: How to read  and convert single precision 32 bit float (IEEE754) from a stream
Currently Being Moderated

Who knows?

I assumed OP meant they had a byte stream where each 4 bytes was the byte array of a float.

As if that byte array had been created like this:

float myFloat = 123.456;

byte [] ba = ByteBuffer.allocate(4).putFloat(myFloat).array();

• ###### 5. Re: How to read  and convert single precision 32 bit float (IEEE754) from a stream
Currently Being Moderated

rp0428, PhHein

Thank you both for the comments.

rp0428, answers (0) and (1), I wholeheartedly accept

but (2), largely because of my own lack of understanding, I want to convert to "familiar looking" numbers.

Also, in part because I need to graph them (they are amplitudes of signals, from some device, that's all I know) and I thought they look better in 10 based numbers.

I will also google, but if you know it right off, a line or two of Java code would be nice.

I am reading a multipart MIME file that has two different kinds of data: one part is UTF-8 encoded, which I can read using any text editor, the other part contains binary data,  which is IEEE-754, 32-bit  "single format" floating-point, with the byte order of little endian. It is first time for me to come across with this situation, so I am totally confused. For example, is there "normal" float and IEEE-754 float? Even this kinds of questions. I will have a lot to read.

I found a Hex editor (FlexHex) that shows decimal values as well. I want to make sure that the tool is displaying in little endian order and it is IEEE-754 single precision float. Since I already have the hex string (read directly from the editor), I will test with a java program. My next phase is get the bytes (float bytes) from stream and concatenate 4 of them and convert to decimal (or long, I am already confused) and compare the stream version's answer and the FlexHex's answer.

• ###### 6. Re: How to read  and convert single precision 32 bit float (IEEE754) from a stream
Currently Being Moderated

Following gets a java float into a 4 byte ('int') value in IEEE-754 format.

http://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#floatToIntBits%28float%29

Following goes the other way

http://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#intBitsToFloat%28int%29

Java is big endian so you would need to switch the bytes around.

#### Legend

• Correct Answers - 10 points