Skip to content

Instantly share code, notes, and snippets.

@RickKimball
Last active October 17, 2017 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RickKimball/0d690d6eec71d893cba439fd4b3e099e to your computer and use it in GitHub Desktop.
Save RickKimball/0d690d6eec71d893cba439fd4b3e099e to your computer and use it in GitHub Desktop.
arduino int64_t streaming serial print example
// sketch_oct16a
// printll provides printing for int64_t uint64_t types using Streaming..
// Rick Kimball
#include "Streaming.h"
char * _print_base(char *buf, uint64_t n, int base) {
char *str = &buf[(64+1) - 1]; // handle printing bits
*str = '\0'; // work from least significant digit to most
do {
unsigned digit = n % base;
*--str = digit < 10 ? digit + '0' : digit + 'A' - 10;
} while ( (n /= base) );
return (str);
};
char * printll(char *work, int64_t ldata, int base = DEC)
{
char *ptr = work;
if ( base == DEC && ldata < 0 ) {
ldata = -ldata;
ptr = _print_base(&ptr[1], (uint64_t)ldata, base);
*--ptr = '-';
}
else {
ptr = _print_base(ptr, (uint64_t)ldata, base);
}
return ptr;
}
//---------------------------------------------------------------
const int64_t foos[] =
{
-32767,-32768,-32767,-32766,-32765,-32764,
-5,-4,-3,-2, -1, 0, 1, 2, 3, 4, 5,
0x7FFFFFFFFFFFFFFE,0x7FFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFE,0xFFFFFFFFFFFFFFFF,
};
void setup() {
Serial.begin(9600);
while(!Serial)
;
delay(3000);
Serial << endl << endl << "int64_t print test" << endl;
}
static unsigned indx=0;
void loop() {
int64_t foo = foos[indx++];
char work1[64+1];
Serial << "foo=" << printll(work1,foo,DEC) << endl;
Serial << " 0x" << printll(work1,foo,HEX) << endl;
Serial << " \\0" << printll(work1,foo,OCT) << endl;
Serial.print(" 0b"); Serial.println(printll(work1,foo,BIN));
if ( indx == sizeof(foos)/sizeof(foos[0]))
while(1);
//delay(1000);
}
#if 0 // output
int16_t print test
foo=-32767
0xFFFFFFFFFFFF8001
\01777777777777777700001
0b1111111111111111111111111111111111111111111111111000000000000001
foo=-32768
0xFFFFFFFFFFFF8000
\01777777777777777700000
0b1111111111111111111111111111111111111111111111111000000000000000
foo=-32767
0xFFFFFFFFFFFF8001
\01777777777777777700001
0b1111111111111111111111111111111111111111111111111000000000000001
foo=-32766
0xFFFFFFFFFFFF8002
\01777777777777777700002
0b1111111111111111111111111111111111111111111111111000000000000010
foo=-32765
0xFFFFFFFFFFFF8003
\01777777777777777700003
0b1111111111111111111111111111111111111111111111111000000000000011
foo=-32764
0xFFFFFFFFFFFF8004
\01777777777777777700004
0b1111111111111111111111111111111111111111111111111000000000000100
foo=-5
0xFFFFFFFFFFFFFFFB
\01777777777777777777773
0b1111111111111111111111111111111111111111111111111111111111111011
foo=-4
0xFFFFFFFFFFFFFFFC
\01777777777777777777774
0b1111111111111111111111111111111111111111111111111111111111111100
foo=-3
0xFFFFFFFFFFFFFFFD
\01777777777777777777775
0b1111111111111111111111111111111111111111111111111111111111111101
foo=-2
0xFFFFFFFFFFFFFFFE
\01777777777777777777776
0b1111111111111111111111111111111111111111111111111111111111111110
foo=0
0x0
\00
0b0
foo=1
0x1
\01
0b1
foo=2
0x2
\02
0b10
foo=3
0x3
\03
0b11
foo=4
0x4
\04
0b100
foo=5
0x5
\05
0b101
foo=9223372036854775806
0x7FFFFFFFFFFFFFFE
\0777777777777777777776
0b111111111111111111111111111111111111111111111111111111111111110
foo=9223372036854775807
0x7FFFFFFFFFFFFFFF
\0777777777777777777777
0b111111111111111111111111111111111111111111111111111111111111111
foo=-2
0xFFFFFFFFFFFFFFFE
\01777777777777777777776
0b1111111111111111111111111111111111111111111111111111111111111110
foo=-1
0xFFFFFFFFFFFFFFFF
\01777777777777777777777
0b1111111111111111111111111111111111111111111111111111111111111111
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment