Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > float or double's two's complement representation.

Reply
Thread Tools

float or double's two's complement representation.

 
 
Rajesh.Rapaka
Guest
Posts: n/a
 
      06-13-2006
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.

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      06-13-2006
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
 
Reply With Quote
 
 
 
 
Rajesh.Rapaka
Guest
Posts: n/a
 
      06-13-2006
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


 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      06-13-2006
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/...g/java/gui/faq
http://www.uni-giessen.de/faq/archiv....java.gui.faq/
 
Reply With Quote
 
Rajesh.Rapaka
Guest
Posts: n/a
 
      06-13-2006

> 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.

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      06-13-2006
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
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      06-13-2006
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
 
Reply With Quote
 
Rajesh.Rapaka
Guest
Posts: n/a
 
      06-13-2006

> 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.

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      06-13-2006
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
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      06-13-2006
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
 
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
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
1's complement and 2's complement sarathy C++ 22 08-02-2006 05:53 PM
1's complement and 2's complement sarathy C Programming 20 08-02-2006 05:53 PM
sign magnitude, ones complement, two's complement Mantorok Redgormor C Programming 8 10-07-2003 11:52 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