Possible bug in Serial.print(uint32_t,HEX)

bboyes

Well-known member
This will sound strange but it seems that Serial.print(uint32_t, HEX) for a Teensy doesn't properly print the high nibble in HEX format. Here's the test code
uint32_t tui32_2;
tui32_2 = 4294967295UL; // 2**32 -1
Serial.println("Test UI32");
Serial.print(" 0x");
Serial.print(tui32_2, HEX);
Serial.print(" ");
Serial.print(tui32_2);
Serial.println();

Using Teensy ++2 with Teensyduino 1.20 (that was current when I started this particular task) and Arduino 1.06.

Here's the output on a Dueminalove (the non-Teensy I had at hand)
Test UI32
0xFFFFFFFF 4294967295

Here's the Teensy ++2 output:
0xŽFFFFFFF 4294967295

If I make a minor program change and recompile the high nibble is different:
0x3FFFFFFF 4294967295

And the anomaly is repeatable after soft reset or power cycling for that compilation. So it seems the high nibble is getting mangled by some other memory location whose value changes from compile to compile.

I'm writing a driver for the AD7794 24-bit ADC and it returns some 32-bit data which is more meaningful when viewed in hex format, but something seemed haywire. So I wrote a simple test program which only sets and prints a 32-bit unsigned long and the anomaly is still there but apparently only with the Teensy library.

I'll see if I can find a bug in the Teensy Print source and also try with Teensy 1.22.

Bruce
 
Well it seems to be fixed in 1.22:
Test UI32
0xFFFFFFFF 4294967295

I also ran this test code and it is correct on 1.22 (high nibble wrong on 1.20)
for (uint32_t x16 = 0; x16<=255 ; x16++)
{
tui32_2 = ((x16<<24) | (x16<<16) | (x16<<8) | x16);
Serial.print(" 0x");
Serial.print(tui32_2, HEX);
Serial.print("/");
Serial.print(tui32_2);
if (0 == x16%4)
{
Serial.println();
}
else Serial.print(" ");
}

Bruce
 
Any idea how long this has been a bug? Just wondering if I saw this a while ago and didn't realize it. Thanks...
 
Back
Top