#convert multiple chats to a float

24 messages · Page 1 of 1 (latest)

wraith nimbus
#

I have a char array that contains both chars but also other stuff.

Let’s call the array char stuff[69]

stuff[5] = 3F / 00111111 stuff[6] = 80 / 1000000 stuff[7] = 00 / 00000000 stuff[8] = 00 / 00000000

The iee float value for 1 is
3F800000 / 00111111100000000000000000000000

I need to be able to convert those four chats into a float.

This will be done on an esp device using arduino

harsh bronzeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

pseudo raven
wraith nimbus
#

That’s the data portion of the udp packet. The final 4 bytes are an iee float

pseudo raven
#

iee?

green houndBOT
#
Compiler Output
<source>: In function 'int main()':
<source>:3:15: error: unable to find numeric literal operator 'operator""f'
    3 | char fl[4] = {3f, 80, 00, 00};
      |               ^~
Build failed
pseudo raven
#

;compile cpp

unsigned char fl[4] = {0x3f, 0x80, 0x00, 0x00};
float f = (float)*fl;
std::cout << f;
green houndBOT
#
Program Output
63
stone rune
#

;compile c

char fl[4] = {0x3f, 0x80, 0x00, 0x00};
float* f = (float*)fl;
printf("%f", *f);
green houndBOT
#
Program Output
0.000000
stone rune
#

If you do *fl then you are dereferencing the first element in the char array, which gives you 63 (0x3f).

#

Mine's no better because 3f 80 00 00 in big endian should be 1.0

pseudo raven
#

ye

#

;compile cpp

unsigned char fl[4] = {0x3f, 0x80, 0x00, 0x00};
float f = (float)*fl;
memcpy(&f, (float*)fl, 4);
std::cout << f;
green houndBOT
#
Program Output
4.6006e-41
pseudo raven
stone rune
#

You're right ofc.

#

;compile c

char fl[4] = { 0x00, 0x00, 0x80, 0x3f};
float* f = (float*)fl;
printf("%f\n", *f);
green houndBOT
#
Program Output
1.000000
stone rune
#

Alas, there is no such thing as ntohf() in C.

terse fox
harsh bronzeBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.