6

I use Arduino UNO (Arduino 1.8.3).

This is my code:

void setup() { 
    Serial.begin(115200);
}
void loop() { 
    uint64_t num = 9223372036854775807; 
    Serial.print(num); 
    delay(1000);
}

This is error message:

exit status 1

call of overloaded 'print(uint64_t&)' is ambiguous

How do I solve this error?

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
吳偲華
  • 61
  • 1
  • 3
  • Please edit your question to include the *full* and *complete* error output. Including possible informational notes. Preferably just copy it all and paste it into your question (as text), without any modifications. And please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Aug 31 '17 at 06:22
  • 1
    It's a helpful question, at least in its current form. It's exactly what I was looking for. – Robert M. Mar 30 '23 at 19:19

3 Answers3

3

This is how I would do it. It's messy but it gets the job done.

Output from serial console: 9223372036854775807

void print_uint64_t(uint64_t num) {

  char rev[128]; 
  char *p = rev+1;

  while (num > 0) {
    *p++ = '0' + ( num % 10);
    num/= 10;
  }
  p--;
  /*Print the number which is now in reverse*/
  while (p > rev) {
    Serial.print(*p--);
  }
}
Gruber
  • 94
  • 4
1

A bit better than printing char by char as suggested somewhere else is to actually build the buffer in reverse and then print it at once.

#include <stdint.h>
char* str( uint64_t num ) {
  static char buf[22];
  char* p = &buf[sizeof(buf)-1];
  *p = '\0';
  do {
    *--p = '0' + (num%10);
    num /= 10;
  } while ( num > 0 );
  return p;
}

char* str( int64_t num ) {
    if ( num>=0 ) return str((uint64_t)num);
    char* p = str((uint64_t)(-num));
    *--p = '-';
    return p;    
}

void setup() {
  int64_t num = -92233720368547758L; 
  Serial.print( str(num) );
}
Something Something
  • 3,999
  • 1
  • 6
  • 21
1

If it's helpful to anyone, I modified this a bit to work with an int64_t (I changed the name of the function, sorry).

char* getInt64Str( int64_t num ) {
  int64_t numAbs = abs(num);
  static char buf[22];
  char* p = &buf[sizeof(buf)-1];
  *p = '\0';
  do {
    *--p = '0' + (numAbs%10);
    numAbs /= 10;
  } while ( numAbs > 0 );
  if( (byte)((num & bit(64))>>63) == 1 ){
    *--p = '-';
  }
  return p;
}
Robert M.
  • 575
  • 5
  • 17