Printing Long Values to Serial for Debugging

I want to print out some hex values longer than 4 hex values long. I'm following a thread from a decade ago and it works perfectly up until 4 values. And recommendations on how to fix this in my code? I know that the value is actually being written into memory properly. I just can't write to Serial correctly. Currently it's chopping off any value above bit 4.

Ex).
0x00FFFF
0x00000 <- Should be 0x010000
0x00001 <- Should be 0x010001

#include <stdio.h>

uint64_t testVal = 0;

void setup(){
 Serial.begin(9600);
 Serial.println("Start!");
}

void loop() {
  if (testVal <= 0xFFFFFF) {
    char charVal[6];
    sprintf(charVal, "%08X", testVal);
    Serial.println(charVal);
    testVal = testVal + 0x10;
  }
}```

Reference Thread: https://forum.arduino.cc/t/how-to-use-serial-print-returning-full-hex-value/41936/6

I think the format specifier should be "%08llX" (if unsigned long longs are even supported), and of course, the buffer needs to be big enough.

uint64_t is long long, and not well supported by AVR based Arduinos.

Which Arduino are you using?

Arduino Uno. Specifically ATMega328. This is all testing work to transfer over to an ESP32 Wroover Module.

This works as expected on an Uno:

long testVal = 0;
void setup() {
  Serial.begin(115200);
  Serial.println("Start!");
}
void loop() {
  if (testVal <= 0xFFFFFF) {
    char charVal[20];
    sprintf(charVal, "%08lX", testVal);
    Serial.println(charVal);
    testVal = testVal + 0x10;
  }
}

Yep that works up through 8 bytes on Uno. AWOL's code works on my ESP32 but not the Uno. Probably something related to support for larger structures. Regardless thanks to you both!

  sprintf(charVal, "%08lX%08lX", (uint32_t)testVal>> 32, (uint32_t)testVal & 0xffffffff);

Uncompiled, untested

Long story short. The "X" parameter is just another reference to unsigned int. Changing the number of bytes requested doesn't change the bits written. "l" is for long. So effectively I needed to print a long int instead of just int.

"X" specifies OUTPUT in unsigned hexadecimal notation, with upper case A-F.

No, 'X' specifies that you print hex digits a-f as uppercase.
If you use 'x', you get a-f as lowercase.

8 bytes is a int64_t or an uint64_t, and would look like 0xFFFFFFFFFFFFFFFF. A long/int32_t/uint32_t will only hold 4 bytes like 0xFFFFFFFF.

Here's a trick to print a 8 byte hex:

void setup() {
  uint64_t i = 0xFEEDFACEDEADBEEF;
  //Serial.println(i,HEX);

  char buff[128];
  sprintf(buff, "0x%lX%lX", (uint32_t)(i >> 32), (uint32_t)( i & 0xFFFFFFFFUL));
  Serial.begin(115200);
  Serial.println(buff);
}

void loop() {
}

Here is another trick to print a 8 byte hex :

void setup()
{
  Serial.begin(9600);
  union myTag
  {
    unsigned long long y;
    unsigned long myData[2];
  };

  myTag data;
  data.y = 0x1234FFFFFFFFFFFF;
  Serial.print(data.myData[1], HEX);
  Serial.println(data.myData[0], HEX);
}

void loop() {}

I think I'd like to see leading zeroes in the format specifier, at least for the least significant part (see reply #8) - your example may work with the data given, but could give misleading results for other values.

You are right. I forgot the leading zeros, and I missed that your answer worked for 64 bit numbers as well as 32 bit numbers.

One reason I like sprintf is that it computes the leading zeros for you. When I see folks zero-pad the numbers by writing code to conditionally Serial.print('0') in something like "01:02:03.000004", why not sprintf(buff,"%02d:%02d:%02d.%06d",hour,min,sec,us);

Tested:

void setup() {
  Serial.begin(115200);
  uint64_t i = 0xFEEDFACEDEADBEEF;
  char buff[128];
  sprintf(buff, "0x%08lX%08lX", (uint32_t)(i >> 32), (uint32_t)( i & 0xFFFFFFFFUL));
  Serial.println(buff);
  i = 0x1;
  sprintf(buff, "0x%08lX%08lX", (uint32_t)(i >> 32), (uint32_t)( i & 0xFFFFFFFFUL));
  Serial.println(buff);
  sprintf(buff, "%02d:%02d:%02d.%06d",1,2,3,4);
  Serial.println(buff);
}

void loop() {
}
0xFEEDFACEDEADBEEF
0x0000000000000001
01:02:03.000004