The Artima Developer Community
Sponsored Link

Java Answers Forum
Displaying byte array as float values

6 replies on 1 page. Most recent reply: Jan 22, 2006 10:46 PM by Badsoul Hay

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Badsoul Hay

Posts: 8
Nickname: bsh
Registered: Jan, 2006

Displaying byte array as float values Posted: Jan 18, 2006 10:44 PM
Reply to this message Reply
Advertisement
I am writing an applet where I have data in byte array. I receive data of type word (2 bytes) , double word( 4 bytes), real (4 bytes). I want to display these on the screen. I can display word and double word as integer values. But I am unable to display real(could be a float) type data.
Example :

I receive 32.71 as a byte array 0x42, 0x02, 0xD7, 0x0A
and -32.71 as 0xC2, 0x02, 0xD7, 0x0A


Badsoul Hay

Posts: 8
Nickname: bsh
Registered: Jan, 2006

Displaying byte array as float values Posted: Jan 19, 2006 12:18 AM
Reply to this message Reply
Can anybody help me with java code for the above problem? So that I can interpret the array of bytes data as float/double and display it on screen.?

For Word and DWord to be displayed as integer values, I convert the bytes to a Hex String and using
Integer.parseInt(String, radix)
I am getting the integer values. Is something similar not possible for converting float values? Is there any other way I can interpret these integer and float values? Please help!

Thanks in advance.

Ja Ja Binks

Posts: 14
Nickname: ds99bwood9
Registered: Apr, 2004

Re: Displaying byte array as float values Posted: Jan 19, 2006 3:42 AM
Reply to this message Reply
From what I remember, can't a float be a very big number with lots of values after the decimal point? If it is then I might be able to suggest something.

Badsoul Hay

Posts: 8
Nickname: bsh
Registered: Jan, 2006

Re: Displaying byte array as float values Posted: Jan 19, 2006 8:47 PM
Reply to this message Reply
The numbers that I will be receiving would be in the IEEE 754 floating point format and 4 bytes each. That would limit the range according to float MAX values.

Yes. The numbers could be big.

Dave Hinton

Posts: 42
Nickname: catbells
Registered: Oct, 2003

Re: Displaying byte array as float values Posted: Jan 20, 2006 5:55 AM
Reply to this message Reply
You will find the classes in java.nio helpful.

Badsoul Hay

Posts: 8
Nickname: bsh
Registered: Jan, 2006

Re: Displaying byte array as float values Posted: Jan 22, 2006 10:44 PM
Reply to this message Reply
I tried using the java.nio without success. Would welcome some suggestion from you.

I tried to use the intBitsToFloat method and got success.

Is this the correct way to do it? Can I make it more efficient?

float byteArraytoFloat( byte[] aBytes)
 
// INPUT : an array of 4 bytes
// OUTPUT: equivalent float value 
 
{
int tempbits=((0xff & aBytes[3]) | ((0xff & aBytes[2]) << 8 ) | ((0xff & aBytes[1]) << 16)|((0xff & aBytes[0]) <<24));
    
float ff = Float.intBitsToFloat(tempbits);
 
return ff;
}
 

Badsoul Hay

Posts: 8
Nickname: bsh
Registered: Jan, 2006

Re: Displaying byte array as float values Posted: Jan 22, 2006 10:46 PM
Reply to this message Reply
Would like to hear your suggestion.

Flat View: This topic has 6 replies on 1 page
Topic: File Server Previous Topic   Next Topic Topic: regarding Client/server communication

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use