Skip to content

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

Closed
wants to merge 1 commit into from
Closed

Add support for 64 bit integer printing #6608

wants to merge 1 commit into from

Conversation

RobTillaart
Copy link

@RobTillaart RobTillaart commented Aug 13, 2017

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

@facchinm facchinm added the Print and Stream class The Arduino core library's Print and Stream classes label Aug 14, 2017
@Ivan-Perez
Copy link
Contributor

Ivan-Perez commented Sep 5, 2017

Should this be added to the String class, so it also supports 64-bit integers?

@RobTillaart
Copy link
Author

RobTillaart commented Sep 5, 2017

@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()

@facchinm
Copy link
Member

This PR was merged in arduino/ArduinoCore-API@3c3b27e , closing here

@facchinm facchinm closed this Sep 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Print and Stream class The Arduino core library's Print and Stream classes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants