Serial.print with leading  zeros

Is there a way to get Serial.print to print out the whole of a variable as binary- with leading zeros so that the message is always the same length of characters. I've been trying with sprintf, but the only way I've got close is as below

#include <stdio.h>
int Sensor = 0;
char buffer[17];
char buffer2[17];
void setup()
{
Serial.begin(9600);
}

void loop()
{
Sensor++;
itoa (Sensor,buffer,2);
sprintf(buffer2, "%016s",buffer);
Serial.println(buffer2);

delay(1000);

}

This gives a binary number in the right position on the serial monitor, but without 0s, just blank space (my understanding of the syntax for sprintf is that the 0 before the 16 tells sprintf to pack 0's at the beginning of the string to make it 16 chars long.

I realise this is probably a daft way to go about it - but I can't see a simpler method.

I was barking up the wrong tree - trying to be too clever. This works fine.

int Sensor = 0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
Sensor++;
if (Sensor < 2) Serial.print(B0);
if (Sensor < 4) Serial.print(B0);
if (Sensor < 8) Serial.print(B0);
if (Sensor < 16) Serial.print(B0);
if (Sensor < 32) Serial.print(B0);
if (Sensor < 64) Serial.print(B0);
if (Sensor < 128) Serial.print(B0);
if (Sensor < 256) Serial.print(B0);
if (Sensor < 512) Serial.print(B0);

Serial.print(Sensor, BIN);
//Serial.print(" ");
//Serial.println(Sensor); print out the ascii value for comparison for checking

delay(100);

}

BTW, I think for the first version, you want to pass the int (sensor) straight to the sprintf() to get it to be zero-padded - I think you can only get space-padding if you're printing a string.

Yeah I had a version working like that - but wanted a binary version and couldn't see a way to get sprintf to convert to binary itself - that was why I was passing it between those two buffers.

Then I realised I was being silly doing it that way.

Just for anybody using the search, here's a cleaner way:

for (int i = 0; i < 16; i++)
{
      if (thisReading < pow(2, i))
            Serial.print(B0);
}

Maybe something like the following, based on your use of sprintf for the binary conversion:

//
// davekw7x
//
void setup()
{
    Serial.begin(9600);
}

int Sensor = 0;
char buffer[17];

void loop()
{
    Sensor++;

    // Convert to binary number as a "string" so that we can know
    // how many significant bits there are
    itoa (Sensor, buffer, 2);
    
    // Loop to print leading zeros
    for (int i = 0; i < 16-strlen(buffer); i++) {
        Serial.print('0');
    }
    
    // Now print the significant bits
    Serial.println(buffer);

    delay(1000);

}

Or, you can print the bits, one at a time, without all of the sprintf stuff (and without unnecessarily invoking floating point math functions):

//
// davekw7x
//
void setup()
{
    Serial.begin(9600);
}

int Sensor = 0;

void loop()
{
    Sensor++;

    for (unsigned int mask = 0x8000; mask; mask >>= 1) {
        if (mask & Sensor) {
            Serial.print('1');
        }
        else {
            Serial.print('0');
        }
    }
    Serial.println();
    delay(1000);
}

Or, completely equivalent and more succinctly:

//
// davekw7x
//
void setup()
{
    Serial.begin(9600);
}

int Sensor = 0;

void loop()
{
    Sensor++;

    for (unsigned int mask = 0x8000; mask; mask >>= 1) {
        Serial.print(mask&Sensor?'1':'0');
    }
    Serial.println();
    delay(1000);
}

Happy New Year (Solar, that is) to all and to all a Good Night!

Regards,

Dave