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

A

Andy

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
 
R

Roedy Green

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

That question has nothing to do with softwaretools or the JVM, so I
have snipped those newsgroups.


See Float.intBitsToFloat and Float.floatToIntBits
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Andy said:
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.
 
A

Andy

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.
 

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

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top