This code for displaying binary.....

karl101

Member
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.


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.
 
Note: With Teeny serial, you do have printf:
For example I believe your first could be replaced by something like:
Code:
Serial.printf("0x%016b", inNumber);
Your other part can be done like:
Code:
Serial.printf("0x%03X, %i\n", btnsPressed, btnsPressed);
 
There's always printLeadingZeros(myNumber) (pseudocode) and then Serial.print(myNumber, 2), but that's slow.

I don't think you need `btnsBinary[0] = 0`. Did you mean `btnsBinary[16] = 0`?

The following code might be faster, but maybe the compiler already optimizes your version? Compilers are strange and magical beasts. (see also: https://stackoverflow.com/a/53850409 and https://stackoverflow.com/a/6724041)
Code:
void printBinary(uint16_t x) {
  for(int i = 0; i < 16; i++) {
    btnsBinary[15 - i] = '0' + ((x >> i) & 0x01);
  }
  btnsBinary[16] = '\0';
  // Note: It's good practice to terminate in a NUL here, for readability and possible future-proofing
}

Then, continuing @KurtE's logic, you could have:
Code:
printBinary(btnsPressed);
Serial.printf("0b%s\n", btnsBinary);
Serial.printf("0x%03X, %i\n", btnsPressed, btnsPressed);

Or even the following, using constant string concatenation (for illustration purposes; use whatever you think looks nicer):
Code:
printBinary(btnsPressed);
Serial.printf("0b%s\n"
              "0x%03X, %i\n",
              btnsBinary, btnsPressed, btnsPressed);

(Note that there's no "%b" printf() modifier.)
 
Oops sorry,

I forgot there is no %b... I mostly have used %x for hex...

Lots of ways to code: could be something like:

Code:
void setup()
{
  char buffer[17];
  while (!Serial) ;
  Serial.begin(115200);
  uint32_t my_number = 42;
  Serial.println(convertBinary(my_number, buffer));
}

char* convertBinary(uint16_t x, char *buffer) {
  char *pb = buffer;
  for (uint8_t i = 0; i < 16; i++) {
    *pb++ = (x & 0x8000) ? '1' : '0';
    x <<= 1;
  }
  *pb = '\0';
  return buffer;
}
 
Hello

Thanks for the replies, very helpful, I didn't know about the Serial.printf. I've also found that the printf also works with the Adafruit_SSD1306 oled library.

Code:
  display.clearDisplay();
  display.setCursor(0,0);      
  display.printf("0b%s", btnsBinary);  
  display.setCursor(0,10); 
  display.printf("0x%03X, %i", btnsPressed, btnsPressed);
  display.display();

Thanks Again
Karl.
 
Back
Top