formating bin nbr to 16 digits with leading '0's

Hi,

I need to display a word, i.e. 16 bits. The nbr is changing approx. every second and if the actual nbr fits into lets say 15 bits, the first '0' is not displayed via server.print(number, BIN);.

How can I always dispay 16 digits, even if the nbr fits into less bits?
e.g. "0000101010101010" and not "101010101010"?

Thank you!

Look for the sprintf() function. You can specify a format with leading zero.

marco_c:
Look for the sprintf() function. You can specify a format with leading zero.

Unfortunately, I don't think there's a format specifier for binary.

It's relatively simple to perform the conversion, but unless it's absolutely necessary, drop binary and use hex - it's very easy to lose your place when reading a long binary number.

KevKO:
Hi,

I need to display a word, i.e. 16 bits. The nbr is changing approx. every second and if the actual nbr fits into lets say 15 bits, the first '0' is not displayed via server.print(number, BIN);.

How can I always dispay 16 digits, even if the nbr fits into less bits?
e.g. "0000101010101010" and not "101010101010"?

Thank you!

look at the bitRead() function. you have to expand this for an unsigned int look at highbyte() and lowbyte(), but this should get you close:

byte ledPin[8] = {2,3,4,5,6,7,8,9};

void setup()
{
  Serial.begin(115200);
  for (byte i = 0; i < 8; i++)
  {
    pinMode(ledPin[i], OUTPUT);
  }
} 

void loop()
{
  for (int j = 0; j <= 255; j++)
  {
    printMyByte(j);
    for(byte i = 0; i < 8; i++)
    {
      digitalWrite(ledPin[i], bitRead(j, i));
    }
    delay(500);
  }
  for (int j = 0; j <= 255; j++)
  {
    printMyByte(255-j);
    for(byte i = 0; i < 8; i++)
    {
      digitalWrite(ledPin[i], bitRead(255-j, i));
    }
    delay(500);
  }
}

void printMyByte(byte myByte)// prints leading zeroes
{
  for (byte i = 0; i < 8; i++)
  {
    Serial.print(bitRead(myByte, 7 - i));
  }
  Serial.print(" = ");
  Serial.println(myByte, DEC);
}

Thank you! I have to display it in BIN and not HEX, because there are several values decoded within that 16-bit word.

This code works for me:

void printWord ( uint32_t TM_TC_word )
{
  for ( int i = 0; i < 16; i++ )
  {
    if ( i == 1 || i == 4 || i == 5 || i == 13 || i == 15 )
    {
      server.print(".");
    }
    server.print( bitRead (TM_TC_word, 15 - i) );
  }
  server.println();
}

which produces code like: 0.011.1.11011111.10.1
what I wanted the code to do. Thank you :slight_smile:

terrific, thanks for letting us know!

0.011.1.11011111.10.1

can you tell more about this format? (the meaning of the fields).

Well this is a format that is used for a satellite unit, that manages TM and TC (telemetry and telecommands). Each bit or couple of bits represents some settings, that are transmitted to the satellite, or received back from it (like frequency settings, what modulation shall be used, e.g. QPSK, ...)
And of course my simulator is only a very small part of the whole system...

At the moment I am using three Arduinos, one UNO, one MEGA and one Ethernet wich approx 3.000 rows of code, respectively, which communicate via Ethernet, serial, RS422 and MILBUS. I am not allowed to tell u more detail :wink: