?
Solved

store negative float value in byte array using Hex and read back as float

Posted on 2010-01-04
7
Medium Priority
?
664 Views
Last Modified: 2012-05-08
I'm trying to store float value -206800000 or -2.068E8 into a byte array
and read it as a float.

the number in Hex is 0xF3AC7B80

I know I cant just do this as displayed below because the number is negative and it would exceed the limit for a byte value

byte [] hexBytes =  new byte[4];
byte[0] = F3;
byte[1] = AC;
byte[2] = 7B;
byte[3] = 80;

I would think I may have to do the 2's compliment for each negative byte, but not sure of the best way to do this.

I would like to store this number(0xF3AC7B80) in an 4 byte byteArray
and then read it out of the byteArray as the float I need it to be so that I end up
with the value -2.068E8

I will be receiving this data from UDP packets
0
Comment
Question by:mitchguy
  • 5
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 26169773
>>I know I cant just do this as displayed below because the number is negative and it would exceed the limit for a byte value

That's not important. In hex, a byte can contain up to 0xFF

If you're working with packets, using java.nio.ByteBuffer would make it fairly easy
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 26169777
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:mitchguy
ID: 26170572
I'll check the links out.

I also have already tried using the byte buffer to read the float, but I don't think I've stored it correctly.
I was getting a loss of precision error with code below.

byte [] hexBytes =  new byte[4];
byte[0] = F3;
byte[1] = AC;
byte[2] = 7B;
byte[3] = 80;

If I force my way past the error by casting to a byte

byte [] hexBytes =  new byte[4];
byte[0] = (byte)F3;
byte[1] = (byte)AC;
byte[2] = (byte)7B;
byte[3] = (byte)80;

Then use a byte buffer

ByteBuffer buf = ByteBuffer.wrap(hexBytes);
System.out.println("BUF:" + buf.getFloat());

I get the output:           -2.733093E31        when I should have     -2.068E8


Maybe I'm using the ByteBuffer incorrectly?


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 26170647
>>Maybe I'm using the ByteBuffer incorrectly?

Well it'd be better to use it for both the put and the get
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 26170697
e.g.
	float val =  -2.068E8F;
	byte[] packet = new byte[4];
	ByteBuffer bb = ByteBuffer.wrap(packet);
	System.out.printf("Putting float %f\n", val);
	bb.putFloat(val);

	bb.rewind();
	System.out.printf("Got float %f\n", bb.getFloat());

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 26189483
:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 4 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question