How to send and receive serial data without transmitted number getting modified?

Dear Arduino Users,

I want to send some numbers over USB and then perform certain actions based on the numbers being sent. I am experimenting with Serial.read() and using the very simple program below:

int incomingByte = 0;   // for incoming serial data

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

void loop() {
  // put your main code here, to run repeatedly:
        if (Serial.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial.read();

                // say what you got:
                Serial.print("I received (DEC): ");
                Serial.println(incomingByte, DEC);
                Serial.print("I received (BIN): ");
                Serial.println(incomingByte, BIN);
        }
}

I then use the "Serial Monitor" to send the number 1 and 2. This is what I get:

I received (DEC): 49
I received (BIN): 110001
I received (DEC): 50
I received (BIN): 110010

Why are the "11" being added in front of both bit-streams?
Why are there only 6-bits, shouldn't there be 8-bits as the system is reading a byte(8-bits) of data?

Like I said in the beginning of my question, I want to perform certain actions based on number being sent, but if the number changes due to extra bits being added, I cannot perform my required action as the comparison fails.

Thanks for the help.

0011 0001 = 49 decimal. 0011 0010 = 50 decimal. Serial print removes (doesn't print) leading zeros.

Numeral "1" = ASCII code 49, "2" = 50, if you want to see 1 or 10 subtract 48 (ASCII code for "0"). Like:

// say what you got:
Serial.print("I received (DEC): ");
Serial.println(incomingByte - 48, DEC);
Serial.print("I received (BIN): ");
Serial.println(incomingByte - '0', BIN);

All keyboard communications are done in a code called ASCII. This represents key strokes with a single byte number. So when you press the key with a '1' written in it the computer sends the code for that key, which happens to be in binary the number you sent in the lower four buts plus a 0011 in the upper four bits.
This code was devised for mechanical tellytype machines that used punched paper tape. It is almost universally used in computing.
Try sending other key strokes you will find an A is represented by the number 0x41 in hex or 01000001 in binary.

You can also look them up here

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

yankeemike:
I then use the "Serial Monitor" to send the number 1 and 2. This is what I get:

I received (DEC): 49

I received (BIN): 110001
I received (DEC): 50
I received (BIN): 110010

When you type something into the serial monitor, what actually gets sent is the ASCII code for whatever you typed followed by a line break. That is, the number is sent as text, not as a number. 49 is the ASCII code for the digit '1', 50 is the asci code for the digit '2', and - for instance - 64 is the ASCII code for a lowercase letter 'a'. I think.

The atoi() function in the standard library converts text numbers into integer values. You have to pass it a string, whch is to say an array of char containing the digits and terminated with a zero (an actual zero, an ASCII NUL character).

If it's just a single digit, then the easiest way to get the number is just to subtract the number for nought, which in C you get just by using '0' as a constant.

  incomingByte = Serial.read();
  digitValue = incomingByte - '0';