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

Discussion in 'Java' started by Andy, May 7, 2004.

  1. Andy

    Andy Guest

    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
     
    Andy, May 7, 2004
    #1
    1. Advertising

  2. Re: need code to convert float format to internal java float formatwhich is kept in 4 bytes integer

    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/api/java/lang/Float.html#floatToIntBits(float)

    --
    Thomas<dot>Fritsch<squiggle>ops<dot>de
     
    Thomas Fritsch, May 7, 2004
    #2
    1. Advertising

  3. Andy

    Roedy Green Guest

    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.
     
    Roedy Green, May 7, 2004
    #3
  4. Andy

    Roedy Green Guest

    On Fri, 07 May 2004 18:15:44 GMT, Roedy Green
    <> 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.
     
    Roedy Green, May 7, 2004
    #4
  5. Andy

    Andy Guest

    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/api/java/lang/Float.html#floatToIntBits(
    float)
    >
    > --
    > Thomas<dot>Fritsch<squiggle>ops<dot>de
    >
     
    Andy, May 8, 2004
    #5
  6. Re: need code to convert float format to internal java float formatwhich is kept in 4 bytes integer

    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/api/java/lang/Float.html#floatToIntBits(
    > float)
    >
    >>--
    >>Thomas<dot>Fritsch<squiggle>ops<dot>de
    >>

    >
    >
    >



    --
    Daniel Sjöblom
    Remove _NOSPAM to reply by mail
     
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, May 9, 2004
    #6
  7. Andy

    Andy Guest

    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.
     
    Andy, May 10, 2004
    #7
  8. Andy

    Roedy Green Guest

    Roedy Green, May 10, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ubi_comp
    Replies:
    0
    Views:
    2,257
    ubi_comp
    Feb 3, 2006
  2. bd
    Replies:
    0
    Views:
    659
  3. Carsten Fuchs
    Replies:
    45
    Views:
    1,627
    James Kanze
    Oct 8, 2009
  4. Steven D'Aprano
    Replies:
    8
    Views:
    418
    Helmut Jarausch
    Jan 21, 2010
  5. Sam Roberts
    Replies:
    2
    Views:
    115
    Carlos
    Nov 26, 2004
Loading...

Share This Page