-3

I have a byte array with length 4

byte[] h = new byte[4];
h[0] = 0x13;
h[1]=(byte)0xBF;
h[2]=(byte)0x71;
h[3]=(byte)0xA8;

which is a latitude value (actual value is 33.1313576) i am converting it into float with following statement:

ByteBuffer.wrap(h).getFloat()

output is 4.8327252E-27

babravahan
  • 113
  • 1
  • 1
  • 7
  • 3
    Show us how you got from `33.1313576` to a `byte[]`. – Sotirios Delimanolis Sep 26 '14 at 03:43
  • we get it through a endpoint creation logic is not known. – babravahan Sep 26 '14 at 03:49
  • 2
    So how do you know it's meant to represent that latitude? – Sotirios Delimanolis Sep 26 '14 at 03:50
  • 1
    the binary representaton in single precision float of 4.8327252E-27 is 0x13bf71a8, and 33.1313576 is 0x42048683. Check it [here](http://www.h-schmidt.net/FloatConverter/IEEE754.html). How do you expect those bytes to be 33.1313576? – phuclv Sep 26 '14 at 04:02
  • 13BF71A8 Latitude (33.1313576) BA18A506 Longitude (-117.2790010) iam getting these in upd packets – babravahan Sep 26 '14 at 04:13
  • @babravahan It's UDP, not 'upd'. I think *you* need to check some issues yourself. You also need to be a lot more specific. "Check how C language interprets 33.1313576" is meaningless in the absence of C code, and without specifying the platform, or the UDP protocol you're using. As a matter of fact 0x13BF71A8 = 331313576 without any scaling. – user207421 Sep 26 '14 at 04:17
  • found a similar issue in http://stackoverflow.com/questions/19408447/longitude-reading-measured-in-degrees-with-a-1x10-7-degree-lsb-signed-2-s-comp but here they are converting in javascript but what iam looking for is Java – babravahan Sep 26 '14 at 04:19
  • Come off it. The logic is identical, and the syntax nearly so. You just have to convert the hex to binary and then scale. Nothing to do with ByteBuffer whatsoever. – user207421 Sep 26 '14 at 04:21

1 Answers1

2

You are mistaken. The byte representation of 33.1313576f as a float is 0x42048683 according to both Java and this online calculator.

What you actually have is not a float at all, it is an integer, that needs to be scaled by 1/1000000 or whatever it is.

user207421
  • 305,947
  • 44
  • 307
  • 483