Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: This code for displaying binary.....

  1. #1
    Junior Member
    Join Date
    May 2016
    Posts
    9

    This code for displaying binary.....

    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.

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    10,024
    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);

  3. #3
    Senior Member
    Join Date
    Mar 2017
    Location
    Oakland, CA, USA
    Posts
    394
    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.)

  4. #4
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    10,024
    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;
    }

  5. #5
    Junior Member
    Join Date
    May 2016
    Posts
    9
    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.

  6. #6
    Senior Member
    Join Date
    Mar 2017
    Location
    Oakland, CA, USA
    Posts
    394
    Any class that derives (directly or indirectly) from the Print class also has a printf function.

    See: https://github.com/adafruit/Adafruit...SSD1306.h#L126
    And Adafruit_GFX: https://github.com/adafruit/Adafruit...ruit_GFX.h#L15

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •