 # how can I put preceding '0' in a binary number

Greetings,

If I want to print a number 0~255 in binary. for example, I could use Serial.print(100,BIN). And the result is "1100100".

My question is how can I put preceding '0's in front, make it always 8 digits as 01100100? 'print (100,BIN,8)' or the '%08d' way does not work.

thanks

LarryD: Research "sprintf" http://www.cplusplus.com/reference/cstdio/sprintf/ http://www.cplusplus.com/reference/cstdio/printf/

I did, but I cannot find a specifier for binary number... If I am using DEC or HEX, there would be no problem.

Right.

You could write a function that prints 1s and 0s when you examine each bit individually as you shift a 1 from one bit to the next.

LarryD: Right.

You could write a function that prints 1s and 0s when you examine each bit individually as you shift a 1 from one bit to the next.

I see, probably there is no easy way for that. I can use bitRead to read all 8 digits and print them one by one...

``````byte b = yourData

for (byte i = 7; i >= 0; i-- )
{
Serial.print((b >> i) & 0X01);//shift and select first bit
}
``````

Also:

``````byte myByte = 0b01011111;

void setup()
{
Serial.begin(9600);
}
void loop()
{
for(int x = 7; x >= 0; x--)
{