Results 1 to 11 of 11
  1. #1
    floating is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default float to binary string and vice-versa

    Hello,

    I am coming from C/C++. And still have to interface with one C library. The problem is facing is following:

    The C library sends me various strings that contain coded floats or ints: basically, 4 chars represent a float or an int (little-Endian).
    How can I extract a float or a string from a 4-char string? And how can I code a float or an int to a 4-char string?

    What I basically need are following two functions in Java (here in C language):

    Java Code:
    void getMemoryFromFloat(float floatNumber,char memory[4])
    { // floatNumber is input, memory is output
        memory[0]=((char*)&floatNumber)[0];
        memory[1]=((char*)&floatNumber)[1];
        memory[2]=((char*)&floatNumber)[2];
        memory[3]=((char*)&floatNumber)[3];
    }
    
    float getFloatFromMemory(char memory[4])
    { // memory is input
        return(((float*)memory)[0]);
    }
    Thanks for any help!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,318
    Rep Power
    5

    Default Re: float to binary string and vice-versa

    Do you want a string representation of a float (e.g. 1.35) or the 32 bit internal representation of a floating point number converted to four 8 bit character fields (which will probably not be printable ascii)?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    floating is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: float to binary string and vice-versa

    Thanks for the quick reply Jim,

    Yes, I need to convert from/to the 32 bit internal representation of a float or int.

    Thanks

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,318
    Rep Power
    5

    Default Re: float to binary string and vice-versa

    Check out Float.floatToIntBits() and Double.doubleToLongBits() and similar routines. That should get you started.

    Regards,
    Jim
    Last edited by jim829; 05-22-2013 at 04:22 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    floating is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: float to binary string and vice-versa

    Thanks,

    Indeed that's a good start! Now I can convert from float to int. But eventually I will still have to pack my int into a string. How would I do that?

    Thanks

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,318
    Rep Power
    5

    Default Re: float to binary string and vice-versa

    Well, you could do some bit shifting and masking to get 8 bit values. Then put them in a byte array and pass that to a String constructor. That should work but as I have not tried this is in a while you may have to tweak it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default Re: float to binary string and vice-versa

    Have a look at the nio classes Buffer and ByteBuffer and their relevant views (i.e. FloatBuffer, DoubleBuffer etc.)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    floating is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: float to binary string and vice-versa

    Thanks Jim and Jos.

    I tried my hands on the bit-shifting. This is what I have:

    Java Code:
    // From int to String:
    int w=256;
    String s="";
    s+=((char)(w >>> 24)&255);
    s+=((char)(w >>> 16)&255);
    s+=((char)(w >>> 8)&255);
    s+=((char)w&255);
    
    // From String to int:
    int w2=(int)((((char)s.charAt(0)) << 24) + (((char)s.charAt(1)) << 16) + (((char)s.charAt(2)) << 8) + ((char)s.charAt(3)));
    I somehow can't get a "real" zero written as a char in my string. It always ends-up as a 48 char (i.e. "0", not "\0").

    Any idea?

    Thanks

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,304
    Blog Entries
    7
    Rep Power
    20

    Default Re: float to binary string and vice-versa

    Why do you want to have a String of four (possibly) unreadable characters?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    floating is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: float to binary string and vice-versa

    Because I need to exchange that data with a C library that is closed.

    I finally got it to work... here the code:

    Java Code:
    // From float to string:
    int w=Float.floatToIntBits(99.12345678f);
    String s="";
    s+=Character.toString((char)((w >>> 24)&255));
    s+=Character.toString((char)((w >>> 16)&255));
    s+=Character.toString((char)((w >>> 8)&255));
    s+=Character.toString((char)(w&255));
    
    // From string to float:
    int w2=(int)((s.charAt(0) << 24) + (s.charAt(1) << 16) + (s.charAt(2) << 8) + s.charAt(3));		
    float w3=Float.intBitsToFloat(w2);
    Thanks for the helpful advices!

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,318
    Rep Power
    5

    Default Re: float to binary string and vice-versa

    Are you writing any C code yourself? Or just Java?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 5
    Last Post: 02-16-2012, 01:35 AM
  2. Replies: 5
    Last Post: 04-16-2009, 06:30 PM
  3. converting image to binary code n vice versa
    By nupurashi in forum Advanced Java
    Replies: 4
    Last Post: 04-01-2009, 07:27 PM
  4. Replies: 0
    Last Post: 03-17-2009, 07:40 AM
  5. Postfix into prefix and vice versa
    By sfe23 in forum New To Java
    Replies: 9
    Last Post: 02-19-2009, 10:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •