krupski
December 13, 2013, 7:59pm
1
Nope, it's not the dreaded question mark from trying to print a float... although I suspect it may have a similar root cause.
Can anyone tell me why this fails:
// code ////////////////////////////
uint32_t value = 0x12345678;
char buffer [32];
sprintf (buffer, "Value is 0x%08X\r\n", value);
Serial.print (buffer);
LCD.print (buffer);
Anything.print (buffer);
/// code ///////////////////////////
.... all yield "Value is 0x00005678"
Instead of the expected "0x12345678"?
Now, to say it upfront... to save us all time... please... I am not looking for suggestions to use "print (value, HEX)", questions like "why do ya wanna do it that way?" or any of the other AVR/Arduino specific goofys used to work around disabled standard C features. I would like to know how to make the ABOVE sprintf example work.
If this means linking in a different library at the expense of code size (like the floating point thing) THAT'S WHAT I WANT.
I do not want to do a half dozen "Serial.print (this)" and "Serial.print (that)" just to build up one little line of text.
Thank you, and I will greatly appreciate any help here.
-- Roger
system
December 13, 2013, 8:11pm
2
Use "%lX" for "Long Hex" (that's lower-case L).
Confirmed:
void setup ()
{
Serial.begin (115200);
Serial.println ();
uint32_t value = 0x12345678;
char buffer [32];
sprintf (buffer, "Value is 0x%08lX", value);
Serial.println (buffer);
} // end of setup
void loop () { }
Output:
Value is 0x12345678
krupski
December 13, 2013, 8:43pm
4
majenko:
Use "%lX" for "Long Hex" (that's lower-case L).
Awesome! Thanks so much... it works of course.
Now a question... why do I have to do this? Using GCC in Linux a simple "%08X" prints properly.
It is related to default INT sizes? maybe?
Anyway, thanks for the answer!
-- Roger
system
December 13, 2013, 8:45pm
5
Krupski:
majenko:
Use "%lX" for "Long Hex" (that's lower-case L).
[quote author=Nick Gammon link=topic=204541.msg1506050#msg1506050 date=1386965964]
Confirmed:
Awesome! Thanks so much... it works of course.
Now a question... why do I have to do this? Using GCC in Linux a simple "%08X" prints properly.
It is related to default INT sizes? maybe?
Anyway, thanks for the answer!
-- Roger
[/quote]
Yep, you got it. On a PC your int will be 32-bits. On the Arduino it's only 16 bits. And %X prints an int, not a long.
Krupski:
Now a question... why do I have to do this? Using GCC in Linux a simple "%08X" prints properly.
It is related to default INT sizes? maybe?
Indeed, whenever you see "int" (compared to a PC/Mac/Linux) that should be a red flag. The size is different. Ditto for pointers.