Serial.print strange problem

Hello guys,
i have recently run into this problem, and i don't know why it happens. If i use this code, everything works fine:

if(Serial.available()){
    for(i=0;i<3;i++){
      incoming=Serial.read();
      buffer[i]=incoming;
      Serial.println(buffer[i]);
    }
    Serial.println(buffer[0]& 0xf);
    Serial.println(buffer[1]& 0xf);
    Serial.println(buffer[2]& 0xf);
    
  }

and, if i send "123", output is like:
49
50
51
1
2
3

but....if i take off this line Serial.println(buffer[i]); (which is found in the for routine in the above code, it stops working and i get data like:
1
15
15
2
3
15

(data sent is still "123"). I have no idea why this happens! Btw, 0xf is 15 in decimal.

Thank you :=)

Because the "ln" bit of the print statement tells the computer to output the ASCII codes that put it on a new line, that is a carriage return and a line feed. (CR & LF)

I still don't get why that doesn't also happen in the first code though...
and why there are two, and not one, 15s after the "1" is "outputted".

Serial.read() returns -1 (0xffff) if there is no character available. You are probably seeing the masked-off 0xf from the -1.

Suppose Serial.available() returns 1 and there really is only one character available. Then the code reads three bytes. One will be the value, and two will be (int) -1, thus printed as 15.

-br

http://entropymouse.com

but...why doesn't that happen when the first Serial.println is there?

The Serial.println takes time, so the buffer can fill up with the next bytes you need to read. Correct would be, as stated before a few times:

if(Serial.available() [glow]>= 3[/glow]){

Korman