float or double's two's complement representation.

Discussion in 'Java' started by Rajesh.Rapaka, Jun 13, 2006.

  1. Hi,

    I need to copy a float value into a byte array. For which I used to do
    the left and right shiftings in case of int.

    I believe there is no 2's compliment defined for float.

    but then how can we write a float value to a network? I need to get the
    bytes of a float value to store it into a byte array and then write the
    whole array to the network.
    (the array contains various other datatypes. thus the byte array
    format).

    any help?

    Thanks in advance.
    Rajesh rapaka.
    Rajesh.Rapaka, Jun 13, 2006
    #1
    1. Advertising

  2. On 13 Jun 2006 00:10:06 -0700, Rajesh.Rapaka wrote:
    > I need to get the bytes of a float value to store it into a byte
    > array and then write the whole array to the network.


    You could start by reading about Float.floatToIntBits(), for example.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Jun 13, 2006
    #2
    1. Advertising

  3. Well, I tried this out. But it doesnt give me the correct output.
    Gordon Beaton wrote:
    > On 13 Jun 2006 00:10:06 -0700, Rajesh.Rapaka wrote:
    > > I need to get the bytes of a float value to store it into a byte
    > > array and then write the whole array to the network.

    >
    > You could start by reading about Float.floatToIntBits(), for example.
    >
    > /gordon
    >
    > --
    > [ do not email me copies of your followups ]
    > g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Rajesh.Rapaka, Jun 13, 2006
    #3
  4. Do not top-post. Reordered for readability.

    Rajesh.Rapaka wrote:
    > Gordon Beaton wrote:
    >> You could start by reading about Float.floatToIntBits(), for example.

    > Well, I tried this out. But it doesnt give me the correct output.


    You never specified what you would consider as "correct".
    floatToIntBits() does give you the bit pattern of the float value. It is
    correct in that sense. If this is not what you want, rephrase your question.

    /Thomas
    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
    Thomas Weidenfeller, Jun 13, 2006
    #4

  5. > Do not top-post. Reordered for readability.

    what does this mean? (i am using this from google groups).

    > You never specified what you would consider as "correct".


    This could be correct but as i said, I need to dissolve it into the
    byte format and reconstruct the same to float. ( as I said i'll need to
    store it into a byte array).

    > floatToIntBits() does give you the bit pattern of the float value. It is
    > correct in that sense. If this is not what you want, rephrase your question.


    hope this simple example may show my problem clearly...

    float f = 102.3f;
    // making a byte array of the float value (the other method in
    discussion is also giving the same result as this.)

    ByteArrayOutputStream bas = new ByteArrayOutputStream();
    DataOutputStream das = new DataOutputStream(bas);
    das.writeFloat(f);
    byte[] barr = bas.toByteArray();

    // making the float again from the byte arrays.
    BigInteger bi = new BigInteger(barr);
    float ff = bi.floatValue();
    System.out.println("float val is " + ff);

    -------------------------------------

    output = 1.1207049E9
    --------------------------------------
    Am I going wrong somewhere?

    thanks in advance.

    regards,
    Rajesh rapaka.
    Rajesh.Rapaka, Jun 13, 2006
    #5
  6. On 13 Jun 2006 03:50:06 -0700, Rajesh.Rapaka wrote:
    > hope this simple example may show my problem clearly...
    >
    > float f = 102.3f;
    > // making a byte array of the float value (the other method in
    > discussion is also giving the same result as this.)
    >
    > ByteArrayOutputStream bas = new ByteArrayOutputStream();
    > DataOutputStream das = new DataOutputStream(bas);
    > das.writeFloat(f);
    > byte[] barr = bas.toByteArray();
    >
    > // making the float again from the byte arrays.
    > BigInteger bi = new BigInteger(barr);
    > float ff = bi.floatValue();
    > System.out.println("float val is " + ff);
    >
    > -------------------------------------
    >
    > output = 1.1207049E9
    > --------------------------------------
    > Am I going wrong somewhere?


    Yes, you are using completely unrelated operations to encode and
    decode the value.

    Since you use DataOutputStream.writeFloat() to write the float, you
    should read it back with DataInputStream.readFloat().

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Jun 13, 2006
    #6
  7. Rajesh.Rapaka wrote:
    >> Do not top-post. Reordered for readability.

    > what does this mean? (i am using this from google groups).
    >
    >> You never specified what you would consider as "correct".

    >
    > This could be correct but as i said, I need to dissolve it into the
    > byte format and reconstruct the same to float. ( as I said i'll need to
    > store it into a byte array).
    >
    >> floatToIntBits() does give you the bit pattern of the float value. It is
    >> correct in that sense. If this is not what you want, rephrase your question.

    >
    > hope this simple example may show my problem clearly...
    >
    > float f = 102.3f;
    > // making a byte array of the float value (the other method in
    > discussion is also giving the same result as this.)
    >
    > ByteArrayOutputStream bas = new ByteArrayOutputStream();
    > DataOutputStream das = new DataOutputStream(bas);
    > das.writeFloat(f);
    > byte[] barr = bas.toByteArray();
    >
    > // making the float again from the byte arrays.
    > BigInteger bi = new BigInteger(barr);
    > float ff = bi.floatValue();
    > System.out.println("float val is " + ff);
    >
    > -------------------------------------
    >
    > output = 1.1207049E9
    > --------------------------------------
    > Am I going wrong somewhere?
    >
    > thanks in advance.
    >
    > regards,
    > Rajesh rapaka.
    >


    You actually have two options, but regardless of which you choose, as
    Gordon Beaton has pointed out, you MUST make decoding exactly reverse
    the encoding.

    1. Write the bit pattern. The float will take exactly four bytes,
    regardless of its value. The float will look meaningless in the file
    unless you also study the internal structure of float.

    2. Use Float.toString(float) and Float.valueOf(String). The length of
    the string will depend on the value of the float, and will often be
    longer than four bytes. It will be human readable in the file.

    Patricia
    Patricia Shanahan, Jun 13, 2006
    #7

  8. > You actually have two options, but regardless of which you choose, as
    > Gordon Beaton has pointed out, you MUST make decoding exactly reverse
    > the encoding.


    This means that I make and a server and it would not support clients
    from different platforms?? or I make a server with java and a 3rd party
    client made with java would not work even though everyting is correct
    just because it used a different technique?

    Though as you've said exactly the reverse is working, but isnt it
    technique dependent?

    Plz do suggest me any docs in case I am missing the whole point.

    thanks in advance,
    Rajesh Rapaka.
    Rajesh.Rapaka, Jun 13, 2006
    #8
  9. On 13 Jun 2006 06:22:38 -0700, Rajesh.Rapaka wrote:
    > This means that I make and a server and it would not support clients
    > from different platforms?? or I make a server with java and a 3rd party
    > client made with java would not work even though everyting is correct
    > just because it used a different technique?


    If you want to use clients from different technologies, you need to
    agree on a standard way of representing your data.

    Float.floatToIntBits() gives you the starting point you need, it
    returns the contents of the float in a well-defined format. Once you
    have those bits, you can shift and mask until you've massaged the data
    into a format you like. Did you read the documentation for that
    method?

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Jun 13, 2006
    #9
  10. Gordon Beaton wrote:
    > On 13 Jun 2006 06:22:38 -0700, Rajesh.Rapaka wrote:
    >> This means that I make and a server and it would not support clients
    >> from different platforms?? or I make a server with java and a 3rd party
    >> client made with java would not work even though everyting is correct
    >> just because it used a different technique?

    >
    > If you want to use clients from different technologies, you need to
    > agree on a standard way of representing your data.
    >
    > Float.floatToIntBits() gives you the starting point you need, it
    > returns the contents of the float in a well-defined format. Once you
    > have those bits, you can shift and mask until you've massaged the data
    > into a format you like. Did you read the documentation for that
    > method?


    In non-java environments, you may need to know that Float.floatToIntBits
    gives the bit pattern representing the float as a 32-bit IEEE 754 binary
    floating point number.

    For java-to-java, even across platforms, it will all just work as long
    as the code doing the decoding is consistent with the encoding method.

    Patricia
    Patricia Shanahan, Jun 14, 2006
    #10
  11. Rajesh.Rapaka

    Chris Smith Guest

    Rajesh.Rapaka <> wrote:
    > I need to copy a float value into a byte array. For which I used to do
    > the left and right shiftings in case of int.
    >
    > I believe there is no 2's compliment defined for float.


    It is true that floating point numbers don't use two's complement.
    However, you seem to be a little unclear about what two's complement is.
    The two's complement representation for integers is a way to assign a
    specific MEANING to a sequence of bits. Although floating point numbers
    don't use the two's complement scheme, there ARE specifications that
    define representations of floating point numbers as a sequence of bits.

    One of these, and the easiest to use from Java, is IEEE 754. To write
    or read IEEE 754 format floating point numbers, you can do any number of
    things. For example, you could use java.io.DataOutputStream to write to
    a ByteArrayOutputStream.

    --
    Chris Smith - Lead Software Developer / Technical Trainer
    MindIQ Corporation
    Chris Smith, Jun 18, 2006
    #11
  12. Rajesh.Rapaka

    jmcgill Guest

    Chris Smith wrote:

    >
    > One of these, and the easiest to use from Java, is IEEE 754. To write
    > or read IEEE 754 format floating point numbers, you can do any number of
    > things. For example, you could use java.io.DataOutputStream to write to
    > a ByteArrayOutputStream.
    >


    I never figured out how to do the normalizing step without using a loop.
    jmcgill, Jun 18, 2006
    #12
  13. Rajesh.Rapaka

    Dale King Guest

    Patricia Shanahan wrote:
    >
    > You actually have two options, but regardless of which you choose, as
    > Gordon Beaton has pointed out, you MUST make decoding exactly reverse
    > the encoding.
    >
    > 1. Write the bit pattern. The float will take exactly four bytes,
    > regardless of its value. The float will look meaningless in the file
    > unless you also study the internal structure of float.
    >
    > 2. Use Float.toString(float) and Float.valueOf(String). The length of
    > the string will depend on the value of the float, and will often be
    > longer than four bytes. It will be human readable in the file.


    As Patricia knows there is another method as well that might come into
    play if you possibly want to communicate the exact value of the floating
    point value. This is important when you will be going from Java to
    another system that may have larger floating point values (e.g. 128-bit
    floating point values).

    That method is to convert it to BigDecimal and then call toString. This
    will give you the string for the *exact* value represented by the float
    or double. Float.toString and Double.toString do not usually give you
    the *exact* string. They give you the shortest string that when fed to
    the parseFloat or parseDouble routine give you the same float or double
    value.
    --
    Dale King
    Dale King, Jul 6, 2006
    #13
  14. Dale King wrote:
    > Patricia Shanahan wrote:
    >>
    >> You actually have two options, but regardless of which you choose, as
    >> Gordon Beaton has pointed out, you MUST make decoding exactly reverse
    >> the encoding.
    >>
    >> 1. Write the bit pattern. The float will take exactly four bytes,
    >> regardless of its value. The float will look meaningless in the file
    >> unless you also study the internal structure of float.
    >>
    >> 2. Use Float.toString(float) and Float.valueOf(String). The length of
    >> the string will depend on the value of the float, and will often be
    >> longer than four bytes. It will be human readable in the file.

    >
    > As Patricia knows there is another method as well that might come into
    > play if you possibly want to communicate the exact value of the floating
    > point value. This is important when you will be going from Java to
    > another system that may have larger floating point values (e.g. 128-bit
    > floating point values).
    >
    > That method is to convert it to BigDecimal and then call toString. This
    > will give you the string for the *exact* value represented by the float
    > or double. Float.toString and Double.toString do not usually give you
    > the *exact* string. They give you the shortest string that when fed to
    > the parseFloat or parseDouble routine give you the same float or double
    > value.


    I agree. I skipped that one in this thread because I was trying to get
    across the basics, the importance of picking a definite method and using
    it consistently.

    Note that the string resulting from the BigDecimal approach may be very
    long, especially if the double is large or small. There are tricks to
    get around that, if it becomes a problem.

    Patricia
    Patricia Shanahan, Jul 10, 2006
    #14
    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. bd
    Replies:
    0
    Views:
    621
  2. Mantorok Redgormor

    sign magnitude, ones complement, two's complement

    Mantorok Redgormor, Oct 5, 2003, in forum: C Programming
    Replies:
    8
    Views:
    8,597
    Glen Herrmannsfeldt
    Oct 8, 2003
  3. k3n3dy
    Replies:
    15
    Views:
    990
    dan2online
    Apr 20, 2006
  4. Carsten Fuchs
    Replies:
    45
    Views:
    1,539
    James Kanze
    Oct 8, 2009
  5. Kedar Mhaswade
    Replies:
    1
    Views:
    251
Loading...

Share This Page