Hello
I've devised this code for displaying a 16 bit number as binary on a little display with leading zeros. It appears to work, but does anyone have any thoughts or opinions? Here I am using Serial.println instead.
Thanks
Karl.
I've devised this code for displaying a 16 bit number as binary on a little display with leading zeros. It appears to work, but does anyone have any thoughts or opinions? Here I am using Serial.println instead.
Code:
char pout[255] = {};
char btnsBinary[17] = {};
void printBinary(uint16_t inNumber)
{
btnsBinary[0] = 0;
for (uint8_t i=0; i < 16; i++) {
boolean bit = inNumber & (1 << i);
// boolean bit = bitRead(inNumber, i); // this does the same as above
btnsBinary[15-i] = (bit == true ? '1' : '0');
}
}
uint16_t btnsPressed = 1344;
printBinary(btnsPressed);
sprintf(pout, "0b%s", btnsBinary);
Serial.println(pout);
sprintf(pout, "0x%03X, %i", btnsPressed, btnsPressed);
Serial.println(pout);
// output:
// 0b0000010101000000
// 0x540, 1344
Thanks
Karl.