Arduino How can I print the unsigned long long data

12,779

Solution 1

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 );

Solution 2

It can be done as well with this snippet; a little playing with pointers..

  uint64_t pipe = 0x12345ABCD9ULL;//lets assume  the data is 12345ABCD9 hexadecimal
  uint32_t *p = (uint32_t *)&pipe;
  Serial.printf ("%X%08X\n", p[1], p[0]);

Solution 3

According to the Arduino language reference, you can print HEX values like so:

Serial.println(pipe, HEX)

Another option is to create a union type to destruct the long long into two longs:

union longlong {
    long a;
    long b;
}

And then reference the separate longs as:

longlong i = pipe;
Serial.println(i.a, HEX);
Serial.println(i.b, HEX);

See the union trick explained at hackaday.

Share:
12,779
a-f-a
Author by

a-f-a

Updated on June 05, 2022

Comments

  • a-f-a
    a-f-a about 2 years

    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