Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > need code to convert float format to internal java float format which is kept in 4 bytes integer

Reply
Thread Tools

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

 
 
Andy
Guest
Posts: n/a
 
      05-07-2004
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
 
Reply With Quote
 
 
 
 
Thomas Fritsch
Guest
Posts: n/a
 
      05-07-2004
Andy wrote:

>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/...oIntBits(float)

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

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      05-07-2004
On 7 May 2004 07:44:20 -0700, (Andy) wrote or quoted
:

>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

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-07-2004
On Fri, 07 May 2004 18:15:44 GMT, Roedy Green
<roedy-look-on-the-> wrote or quoted :

>
>>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 http://mindprod.com/jgloss/newsgroups.html to figure out which one
to pick.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Andy
Guest
Posts: n/a
 
      05-08-2004
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
> >according to JVM spec
> >Thanks a lot
> >
> >

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

http://java.sun.com/j2se/1.4.2/docs/...loatToIntBits(
float)
>
> --
> Thomas<dot>Fritsch<squiggle>ops<dot>de
>



 
Reply With Quote
 
=?ISO-8859-1?Q?Daniel_Sj=F6blom?=
Guest
Posts: n/a
 
      05-09-2004
Andy wrote:
> 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
>>>according to JVM spec
>>>Thanks a lot
>>>
>>>

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

>
> http://java.sun.com/j2se/1.4.2/docs/...loatToIntBits(
> float)
>
>>--
>>Thomas<dot>Fritsch<squiggle>ops<dot>de
>>

>
>
>



--
Daniel Sjöblom
Remove _NOSPAM to reply by mail

 
Reply With Quote
 
Andy
Guest
Posts: n/a
 
      05-10-2004
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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-10-2004
On 10 May 2004 06:26:11 -0700, (Andy) wrote or
quoted :

>This is code to convert bytes to float.
>I need the reverse of this

Please don't cross-post.

See http://mindprod.com/jgloss/converter.html
Version 3.9 now shows you this.

See http://mindprod.com/jgloss/floatingpoint.html
for more detail.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best way to convert sequence of bytes to long integer Steven D'Aprano Python 8 01-21-2010 11:02 AM
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
how to convert 4 bytes into a float ? Jean-Baptiste PERIN Python 4 02-08-2005 12:23 PM
std method to convert string of bytes into Integer? Sam Roberts Ruby 2 11-26-2004 06:04 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57