Jar0d
1
Hi,
I have 2 float32 variables (latitude and longitude) and I need to convert them in Hex.
documentation:
Send a SIGFOX message: AT$SF=XXXXXXXXXXXX (Hexadecimal value)
ex: AT$SF=AA12341F27AA12341F27
actually I have:
union test {
float fval;
int uival;
} test = { 8.43876f };
char str [50];
sprintf(str,"%x",test.uival);
Serial.print(str);
But it doesn't print the right value
for example
8.43876 -> 41070529 (right output)
529 (arduino output)
can you help me ?
Thanks
hex is only a representation. You want to have a string containing the hex value? Little or big endian?
PS, int is just 16-bit on most Arduino's. Try uint32_T.
Jar0d
3
Yes, Little endian.
Can you show me an code-example ?
Thanks
Try:
union test {
float fval;
uint32_t uival;
} test = { 8.43876f };
char str [50];
sprintf(str,"%lx",test.uival);
Serial.print(str);
Try this (I don't understand sprintf() well)!
void setup()
{
Serial.begin(9600);
union
{
float fval;
long uival; //int uival;
} test = { 8.43876f };
long m = *(long*)&test.fval;
Serial.println(m, HEX); //shows: 41070529
Serial.print(test.uival, HEX); //shows: 41070529
//char str [50]="";;
//sprintf(str, "%x", test.uival);
//Serial.print(str);
}
void loop()
{
}
Do note it's not very portable. Both float and long are nor guaranteed to be 32-bit and/or to be the same size. Also, of course, the endianness.
Jar0d:
529 (arduino output)
When the OP has mentioned arduino, has he not said about a 'standard Arduino -- UNO/NANO/MEGA'?. If so, float and long are 32 bits. Are not they?