Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Virtual Machine / May 2004

Tip: Looking for answers? Try searching our database.

need code to convert float format to internal java float format which is kept in 4  bytes integer

Thread view: 
Andy - 07 May 2004 15:44 GMT
JVM structure CONSTANT_Float_info keeps float value
in 4 bytes integer type. I need code to convert float to 4 bytes
according to JVM spec
Thanks a lot
Thomas Fritsch - 07 May 2004 16:23 GMT
>JVM structure CONSTANT_Float_info keeps float value
>in 4 bytes integer type. I need code to convert float to 4 bytes
>according to JVM spec
>Thanks a lot
>  

float f = ...;
int i = Float.floatToIntBits(f);

See
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Float.html#floatToIntBits(float)

Signature

Thomas<dot>Fritsch<squiggle>ops<dot>de

Andy - 08 May 2004 16:00 GMT
Thanks
But I need piece of code written in C.

> >JVM structure CONSTANT_Float_info keeps float value
> >in 4 bytes integer type. I need code to convert float to 4 bytes
[quoted text clipped - 5 lines]
>
> See

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Float.html#floatToIntBits(
float)

> --
> Thomas<dot>Fritsch<squiggle>ops<dot>de
Daniel Sjöblom - 09 May 2004 01:08 GMT
> Thanks
> But I need piece of code written in C.

Why did you post here then? This is a java group. Anyway (assuming float
and int are both 4 bytes):

unsigned int f_to_ui(float f) { return *((unsigned int *) &f); }
float ui_to_f(unsigned int i) { return *((float *) &i); }

Remember that all of the class file format assumes big endian data,
while your platform might be little endian.

>>>JVM structure CONSTANT_Float_info keeps float value
>>>in 4 bytes integer type. I need code to convert float to 4 bytes
[quoted text clipped - 11 lines]
>>--
>>Thomas<dot>Fritsch<squiggle>ops<dot>de

Signature

Daniel Sjöblom
Remove _NOSPAM to reply by mail

Andy - 10 May 2004 14:26 GMT
This is code to convert bytes to float.
I need the reverse of this

bytes
   The bytes item of the CONSTANT_Integer_info structure represents
the value of the int constant. The bytes of the value are stored in
big-endian (high byte first) order.

   The bytes item of the CONSTANT_Float_info structure represents the
value of the float constant in IEEE 754 floating-point single format
(§3.3.2). The bytes of the single format representation are stored in
big-endian (high byte first) order.

   The value represented by the CONSTANT_Float_info structure is
determined as follows. The bytes of the value are first converted into
an int constant bits. Then:

       * If bits is 0x7f800000, the float value will be positive
infinity.
       * If bits is 0xff800000, the float value will be negative
infinity.
       * If bits is in the range 0x7f800001 through 0x7fffffff or in
the range 0xff800001 through 0xffffffff, the float value will be NaN.
       * In all other cases, let s, e, and m be three values that
might be computed from bits:

       int s = ((bits >> 31) == 0) ? 1 : -1;
       int e = ((bits >> 23) & 0xff);
       int m = (e == 0) ?
               (bits & 0x7fffff) << 1 :
               (bits & 0x7fffff) | 0x800000;

Then the float value equals the result of the mathematical expression
s·m·2e-150.
Pete Kirkham - 10 May 2004 20:24 GMT
> This is code to convert bytes to float.
> I need the reverse of this
...
> Then the float value equals the result of the mathematical expression
> s·m·2e-150.
You have three choices-

1/ perform the opposite operations to what you posted and hope you don't
get too bad rounding errors.

2/ look up the float representation on your machine, use the cast Daniel
Sjöblom gave you, and maybe have to do some additional bit twiddling to
make your machine's representation big-endian IEEE.

3/ in the unlikely event that your machine cannot support IEEE directly,
find and use a IEEE library.

Pete


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.