2

I have long Byte array as

Byte *GolfResult = (Byte*)calloc(40, sizeof(Byte));

Those Bytes are in Hex value.

For every four Bytes I like to convert to float. What I do is,make a four byte array and

unsigned char *fourbytearray = (unsigned char *)calloc(4, sizeof(unsigned char));

Then copy every four bytes to this fourbytearray.

fourbytearray[3] = GolfResult[0]; fourbytearray[2] = GolfResult[1]; fourbytearray[1] = GolfResult[2]; fourbytearray[0] = GolfResult[3];

Then convert to float.

float result = [[NSNumber numberWithUnsignedChar:*bytearray] floatValue];

It is waste of time and the result is not correct.

For example, I have four bytes as 0x4148e7e1. If I convert that hex value to float in this converter, I have correct result as 12.5666.

But if I convert like [[NSNumber numberWithUnsignedChar:*bytearray], the result is 65. It is wrong.

So what would be the best way to convert Byte array to float values for every four Bytes.

3
  • Where do these bytes come from? Commented Jan 19, 2018 at 16:00
  • Come from a sensor. Commented Jan 19, 2018 at 16:05
  • You asked this same question on Jan 9th and accepted an answer. If you now have an issue with that answer comment there (and remove the acceptance of the answer if needed), don't just repeat the question - that goes against the SO MO. Commented Jan 19, 2018 at 20:32

2 Answers 2

0

I agree with meaning-matters. It seems your byte order is reversed with respect to how it should be stored as a floating point number. Since your hex value is just a different representation of the bits that makes up the floating point number, you do not have to do anything more than tell the compiler that it should treat the number as a float. This works once you have placed the bytes in correct order.

fourbytearray[0] = GolfResult[0]; 
fourbytearray[1] = GolfResult[1]; 
fourbytearray[2] = GolfResult[2];
fourbytearray[3] = GolfResult[3];
float result = *(float *)fourbytearray;

I tried your value 0x4148e7e1 and got 12.5566111.

Sign up to request clarification or add additional context in comments.

5 Comments

No it doesn't work. I have 41998DD3. The converter has 19.1943. That casting gave me 0.
you have to take into account endianess
So how to do that?
My query is after copying four bytes to this (unsigned char *fourbytearray), how can I have result in Objective C same as using online converter.
@LGP yes it works.
0

The numberWithUnsignedChar method only converts one byte, the first one. That's why you get 65 which is equal to your first byte 0x41.

This is because *bytearray is the same as bytearray[0]; just the first element of the array. It may help to read through this section of the C-FAQ.

As LGP says, once you have your bytes lined up correctly and made sure the floating point formats match, all you need to do is casting.

6 Comments

Yes I am sure I need to reverse byte-order.
Say if I want to get the result as in converter, how would I do that?
Yes this 4148e7e1 is correct byte-order and if I convert using converter, i have this 12.566 and this is correct. I like to get same result in objective c.
Sorry that is floatValue. I used floatValue for float conversion.
I am testing now.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.