How to create 8 bits to a variable

I'm reading an output signal consiting of 3 (L,M,H) bytes (3x 8 bits) from a sensor.

This output signal however does not always need to be represented by 8 bits.

ex 010101 = 00010101

I would like to force the variable to consist of 8 bits no matter the size of the input number. All input bytes should be represented with an 8-bit length.

This is what my code looks like:

void setup() { // put your setup code here, to run once: Serial.begin(9600);

byte L = 00000000; byte M = 00000000; byte H = 00000000;

}

void loop() { // put your main code here, to run repeatedly:

while (Serial.available()) {

byte L = Serial.read(); byte M = Serial.read(); byte H = Serial.read();

Serial.println(L, BIN); Serial.println(M, BIN); Serial.println(H, BIN);

}

}

byte L = Serial.read();

Taking this as an example, the variable L will always be 8 bits in length.

Serial.println(L, BIN);

Serial.print() does not print leading zeros, hence the variable appears to be only 6 bits in length.

btw, the bytes L, M and H that you have in setup() are different to the bytes L, M and H that you have in loop() - so you are not using the ones in setup().

while (Serial.available()) {

byte L = Serial.read();
byte M = Serial.read();
byte H = Serial.read();

This is flawed.

http://www.gammon.com.au/tips#trap1

You've hit problem #1 in my list!