Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

floating point format conversion

Status
Not open for further replies.

Bones3

Programmer
Jul 27, 2003
151
0
0
US
I'm getting a message from a server which has an intel dual core processor and Windows. The message is supposed to be a bunch of binary single-precision floating point numbers. The size of the numbers appear to be the same as the Java float, but the format does not seem to be the same.

I used some code from a website ( that almost works (i.e. the numbers are relatively close to correct, but still unacceptably off):

Code:
float readFloatLittleEndian()
   {
   int accum = 0;
   for ( int shiftBy=0; shiftBy<32; shiftBy+=8 )
      {
      accum |= ( readByte () & 0xff ) << shiftBy;
      }
   return Float.intBitsToFloat( accum );
   }

I have also tried to write some of my own code, but haven't been too successful not knowing much about binary conversions and number formats. Can anyone help me with this? Have I provided enough information about the machine?

-Bones
 
I have recently been writing an app that needs to read and write binary data in little-endian format, and I ended up using the Struct class in the Javolution library.

You define a C-style struct, and provide a method which returns the ByteOrder from that struct. I'm not sure whether that's a facility provided purely by Javolution or within the standard JDK, might be worth digging around for "ByteOrder" in the JDK docs.

Annihilannic.
 
Is it possible that the exponent (bits 1-8, 0-based) and the mantissa (bits 9-31, 0-based) are separately bit-swapped relative to your processor?

_________________
Bob Rashkin
 
One thing that is particularly bothersome to me is that negative numbers don't work right at all. The negative changes the value by one, but I thought only one bit should change independantly of the others? The binary on the Intel machine looks like this:

3 = 00000000 00000000 01000000 01000000
-3 = 00000000 00000000 01000000 00111111

When I do the conversion to big-endian Java float, -3 becomes 0.75 and +3 becomes 3.0.

-Bones
 
That looks wierd to me. It's almost as if they're doing a 2's compliment on the exponent (but not on the mantissa). You may find this reference helpful:
_________________
Bob Rashkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top