2

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

a-f-a
  • 147
  • 1
  • 3
  • 9

5 Answers5

3

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 );
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
1

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.

Rolf
  • 7,098
  • 5
  • 38
  • 55
1

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]);
karelv
  • 756
  • 9
  • 20
0

Copy your long long number n it into a Float and then print (float,0).

Ale
  • 1
0

This is how I made an Arduino print long long, and unsigned long long integers (in base 10):

void print_unsigned_long_long(unsigned long long val) {
    if (val < 10) {
        Serial.print((unsigned int) val);
    } else {
        unsigned long long subproblem = val / 10;
        int remainder = val % 10;
        print_unsigned_long_long(subproblem);
        Serial.print(remainder);
    }
}

void print_long_long(long long val) {
    long long positive = val;
    if (val < 0) {
        Serial.print("-");
        positive *= -1;
    }
    print_unsigned_long_long(positive);
}


void setup() {

    Serial.begin(9600);

    Serial.println("Unsigned Long Long (Expect 18446744073709551615)");
    print_unsigned_long_long(18446744073709551615ULL);
    Serial.println();
    Serial.println();

    Serial.println("Signed Long Long (Expect -9223372036854775807)");
    print_long_long(-9223372036854775807LL);
    Serial.println();
    Serial.println();

    Serial.println("Signed Long Long (Expect 9223372036854775807)");
    print_long_long(9223372036854775807LL);
    Serial.println();
    Serial.println();

}

This is the output, confirming correct implementation for displaying the maximum boundaries of the signed long long and unsigned long long integers:

Unsigned Long Long (Expect 18446744073709551615)
18446744073709551615

Signed Long Long (Expect -9223372036854775807)
-9223372036854775807

Signed Long Long (Expect 9223372036854775807)
9223372036854775807
tedz2usa
  • 76
  • 3