Serial.print uint64_t in Arduino
Asked Answered
R

3

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?

Rubalcava answered 31/8, 2017 at 6:17 Comment(2)
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.Favored
It's a helpful question, at least in its current form. It's exactly what I was looking for.Oleaceous
H
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--);
  }
}
Harless answered 6/9, 2017 at 17:40 Comment(0)
R
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) );
}
Raymonderaymonds answered 15/2, 2023 at 4:4 Comment(7)
@MooingDuck the question would have been closed anyway. Gotta love SO. So toxic.Raymonderaymonds
@njuffa That is an excellent solution! Bad thing they were about to delete the questionRaymonderaymonds
I wonder how much awesome content like this is suppressed by these jealous admins of StackOverflow. The level of toxicity on the C++ tag in this website is impressive.Raymonderaymonds
I agree. Why is that? The C++ forum on stackoverflow is quite pleasant and helpful.Oleaceous
I wonder if you have some idea how to do this with a signed integer, int64_t?Oleaceous
Yes that is simple. Will update the answer when I’m back in the officeRaymonderaymonds
This is nice, thank you. I don't know why I was testing the leftmost bit instead of just testing for >=0 lol.Oleaceous
O
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;
}
Oleaceous answered 30/3, 2023 at 21:4 Comment(1)
I have added the int64_t implementation on the question above. No need to modify the original.Raymonderaymonds

© 2022 - 2024 — McMap. All rights reserved.