Strange char appened after a copy of a serial read buffer

Hi guys, i m having a strange behaviour using the code below.
If i send for exemple “g26” via serial, the code is returning me

Line: g26
Integer: ‘26ã’
Buffer: ‘26’

instead of

Line: g26
Integer: ‘26’
Buffer: ‘26’

edit: the forum has filtered 2 square chars each side of the ã

void readSerial()
{
  if( !Serial.available() ) return;
  static int i = 0;
  static char Buffer[17];
  char inByte;
  inByte = Serial.read();
  if ( inByte != '\n' ){
    Buffer[i] = inByte;
    i++;
    Buffer[i] = '\0';  
    return;
  }
  Serial.print("Line: ");
  for( int c = 0; c < 16; c++) Serial.print(Buffer[c]);
  Serial.println("");
  char Number[2];
  if ( Buffer[1] == '0' ){
    Number[0] = Buffer[2];
  } else {
    Number[0] = Buffer[1];
    Number[1] = Buffer[2];
  }
  Serial.print("Integer: '");
  Serial.print(Number);
  Serial.println("' ");
  Serial.print("Buffer: '");
  Serial.print(Buffer[1]);
  Serial.print(Buffer[2]);
  Serial.println("'");
  
  for( int c = 0; c < 16; c++) Buffer[c] = 0x00;
  i=0;
  return;

Number is not terminated. (Number does not have enough space for the terminator.)

Hello,

Your string is not valid because it isn't NULL terminated. You have to do that:

char Number[3];

...

Number[2] = '\0';

See this ;)

Thanks guys

The examples in serial input basics may be useful.

...R