-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Add support for 64 bit integer printing #6608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Should this be added to the String
class, so it also supports 64-bit integers?
@Ivan-Perez
The String Class could be extended with two functions (one given here)
String::String(uint64_t value, unsigned char base)
{
init();
char buf[1 + 8 * sizeof(unsigned long)];
ul64toa(value, buf, base);
*this = buf;
}
The AVR stdlib does not have a ul64toa() so you need something like this
// straightforward implementation
void ui64toa(uint64_t v, char * buf, uint8_t base)
{
int idx = 0;
uint64_t w = 0;
while (v > 0)
{
w = v / base;
buf[idx++] = (v - w * base) + '0';
v = w;
}
buf[idx] = 0;
// reverse char array
for (int i = 0, j = idx - 1; i < idx / 2; i++, j--)
{
char c = buf[i];
buf[i] = buf[j];
buf[j] = c;
}
}
A similar function for a signed version void i64toa()
This PR was merged in arduino/ArduinoCore-API@3c3b27e , closing here
Uh oh!
There was an error while loading. Please reload this page.
Added support for printing unsigned long long and long long
solves issue #1236
Included is a reference implementation (commented out) which is a replica of the print code for long.
The code is 2-3x faster with a slightly larger footprint. The performance is gained by working in "16 bit" chunks. Tested on UNO and discussed "long long" ago here - http://forum.arduino.cc/index.php?topic=143584
(minor) Cleanup trailing spaces