Skip to content

Instantly share code, notes, and snippets.

@andrewjaykeller
Created July 25, 2017 20:52
Show Gist options
  • Save andrewjaykeller/8e1d5f794c551dd03ac055a7d70cfaa5 to your computer and use it in GitHub Desktop.
Save andrewjaykeller/8e1d5f794c551dd03ac055a7d70cfaa5 to your computer and use it in GitHub Desktop.
Snippet from OpenBCI_Wifi firmware
/**
* Used to print out a long long number
* @param n {int64_t} The signed number
* @param base {uint8_t} The base you want to print in. DEC, HEX, BIN
*/
String OpenBCI_Wifi_Class::getStringLLNumber(long long n, uint8_t base) {
return String(n < 0 ? "-" : "") + getStringLLNumber((unsigned long long)(-1*n), base);
}
/**
* Used to print out a long long number
* @param n {int64_t} The signed number
*/
String OpenBCI_Wifi_Class::getStringLLNumber(long long n) {
return getStringLLNumber(n, DEC);
}
/**
* Used to print out an unsigned long long number
* @param n {uint64_t} The unsigned number
* @param base {uint8_t} The base you want to print in. DEC, HEX, BIN
*/
String OpenBCI_Wifi_Class::getStringLLNumber(unsigned long long n, uint8_t base) {
unsigned char buf[16 * sizeof(long)]; // Assumes 8-bit chars.
unsigned long long i = 0;
if (n == 0) {
return "0";
}
String output;
while (n > 0) {
buf[i++] = n % base;
n /= base;
}
for (; i > 0; i--) {
output = output + String((char) (buf[i - 1] < 10 ?
'0' + buf[i - 1] :
'A' + buf[i - 1] - 10));
}
return output;
}
/**
* Used to print out an unsigned long long number in base DEC
* @param n {uint64_t} The unsigned number
*/
String OpenBCI_Wifi_Class::getStringLLNumber(unsigned long long n) {
return getStringLLNumber(n, DEC);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment