stringarduinoprintlnunsigned-long-long-int

Arduino How can I print the unsigned long long data


I am trying to print unsigned long long data on Serial monitor but Serial.println() doesn't work because of not a string variable.

So I searched on the internet to convert unsigned long long to String. I came up with some solutions but none of them work. For example;

uint64_t pipe = 0x12345ABCD9LL;//lets assume the data is 12345ABCD9 hexadecimal
char buf[50];
sprintf(buf, "%llu", pipe);
Serial.println( buf );

This code doesn't work. I tried "%lu" , "%lld" as well.

I want to see what my pipe value is. In this case, I want to see 12345ABCD9 on the serial monitor. Is there any other way to do it ? I am waiting your response. Thank you very much.

EDIT:

when I use the "%lu" I see 878361817 variable on the screen(which is not what I want). But the others they are just null , empty


Solution

  • How about dividing it into upper half and lower half?

    Try this (not tested):

    uint64_t pipe = 0x12345ABCD9LL;//lets assume  the data is 12345ABCD9 hexadecimal
    char buf[50];
    if(pipe > 0xFFFFFFFFLL) {
      sprintf(buf, "%lX%08lX", (unsigned long)(pipe>>32), (unsigned long)(pipe&0xFFFFFFFFULL));
    } else {
      sprintf(buf, "%lX", (unsigned long)pipe);
    }
    Serial.println( buf );