It's not a programming question so I post it here instead.

It’s an example of taking serial i/o from terminal, getting values from text and handling unwanted input.
It’s also a decimal to hex and binary converter.
Just be sure that your terminal adds either newline or carriage return when it transmits.

So there it is…

//  Decimal Long Int to HEX & BIN Converter 
//  It -only- expects integers, space delimits, negative values allowed
//  make sure the serial monitor ends lines with newline or carriage return.

char  B; // bytebuffer -- allocated only once
long  V; // for storing number while reading
byte  charCount = 0; // how many digits or bytes of garbage 
byte  stateBits = 0; // bit 0 is sign, bit 1 is reading garbage

void setup() {
  Serial.begin(9600);
  Serial.println("Convert dec to hex and bin");
  Serial.println("Enter digits, - or space through terminal.");
}

void loop(void) { // read one or no character each pass
  if (Serial.available()) {
    charCount += 1;
    B = Serial.read();
    Serial.print(B, BYTE); // echo each character as read
    //    Serial.print(" "); // debuggy informy stuff
    //    Serial.print(B, HEX);
    //    Serial.print(" : ");
    if ( !(stateBits & 2)) { // do if 'garbage in' is NOT set 
      if ((B >= '0') && (B <= '9')) { //// is B a digit?
        if ( charCount > 10 ) { // 
          //        print_value();
          print_error(" overflow ");
          return;
        }
        if (V >= 300000000) { // will adding a digit overflow value?
          //        print_value();
          print_error( " overflow " );
          return;
        }
        // updating V, one digit at a time 
        V *= 10; // shift the previous vale up a decimal place
        V += (B - '0'); // add the new ones
        return;
      } 
      else if ((B == '-') && (charCount == 1))  { // leading minus only
        stateBits |= 1; // V will be made negative at end of read
        charCount = 0; // sign is not a digit
        return;
      }
      else if ((B == ' ') || (B == '\t') || (B == '\r') || (B == '\n'))  { // space & tab delimit
        if (charCount > 1)    { // slice off leading spaces
          print_value();
        }
        else set_zero(); // eat spaces & tabs
        return;
      }
      else {  // whatever gets this far is not a valid character
        //      print_value();
        stateBits |= 2; // garbage-in state set ON
        //      print_error(" long integers please ");
        return;
      }
    }
    else { // garbage-in mode, check for delimiters if so then print error
      if ((B == ' ') || (B == '\t') || (B == '\r') || (B == '\n'))  { 
        //        Serial.println(" decimal integers please!");
        if (charCount > 1)  { // don't println just for a delimiter
          Serial.println(); // just let the garbage echo be the message
          set_zero(); 
        }
        else  charCount = 0; // eat the spaces
      }
      return;
    }
  }
}

void  print_value( void )  { // print dec hex bin
  if (stateBits & 1)    V = -V;
  Serial.print(" > ");
  Serial.print(V, DEC);
  Serial.print(" : 0x");
  Serial.print(V, HEX);
  Serial.print(" : 0b");
  Serial.println(V, BIN);
  set_zero();
}

void print_error(char *errmsg)  {
  /*  if (Serial.available())  {
   while (Serial.available())  {
   B = Serial.read();
   Serial.print(B, BYTE);
   }
   }
   */
  Serial.print(" *error* " );
  Serial.println(errmsg);
  set_zero();
  stateBits = 2; // garbage-in, look for a delimiter then start over
}

void  set_zero( void )  {
  V = 0;      // init, start looking for the next value
  charCount = 0;
  stateBits = 0;
}

rather than quoting it, put it inside [ code ] … [ /code ]

//  Decimal Long Int to HEX & BIN Converter
//  It -only- expects integers, space delimits, negative values allowed
//  make sure the serial monitor ends lines with newline or carriage return.

char  B; // bytebuffer -- allocated only once
long  V; // for storing number while reading
byte  charCount = 0; // how many digits or bytes of garbage
byte  stateBits = 0; // bit 0 is sign, bit 1 is reading garbage

void setup() {
  Serial.begin(9600);
  Serial.println("Convert dec to hex and bin");
  Serial.println("Enter digits, - or space through terminal.");
}

void loop(void) { // read one or no character each pass
  if (Serial.available()) {
    charCount += 1;
    B = Serial.read();
    Serial.print(B, BYTE); // echo each character as read
    //    Serial.print(" "); // debuggy informy stuff
    //    Serial.print(B, HEX);
    //    Serial.print(" : ");
    if ( !(stateBits & 2)) { // do if 'garbage in' is NOT set
      if ((B >= '0') && (B <= '9')) { //// is B a digit?
        if ( charCount > 10 ) { //
          //        print_value();
          print_error(" overflow ");
          return;
        }
        if (V >= 300000000) { // will adding a digit overflow value?
          //        print_value();
          print_error( " overflow " );
          return;
        }
        // updating V, one digit at a time
        V *= 10; // shift the previous vale up a decimal place
        V += (B - '0'); // add the new ones
        return;
      }
      else if ((B == '-') && (charCount == 1))  { // leading minus only
        stateBits |= 1; // V will be made negative at end of read
        charCount = 0; // sign is not a digit
        return;
      }
      else if ((B == ' ') || (B == '\t') || (B == '\r') || (B == '\n'))  { // space & tab delimit
        if (charCount > 1)    { // slice off leading spaces
          print_value();
        }
        else set_zero(); // eat spaces & tabs
        return;
      }
      else {  // whatever gets this far is not a valid character
        //      print_value();
        stateBits |= 2; // garbage-in state set ON
        //      print_error(" long integers please ");
        return;
      }
    }
    else { // garbage-in mode, check for delimiters if so then print error
      if ((B == ' ') || (B == '\t') || (B == '\r') || (B == '\n'))  {
        //        Serial.println(" decimal integers please!");
        if (charCount > 1)  { // don't println just for a delimiter
          Serial.println(); // just let the garbage echo be the message
          set_zero();
        }
        else  charCount = 0; // eat the spaces
      }
      return;
    }
  }
}

void  print_value( void )  { // print dec hex bin
  if (stateBits & 1)    V = -V;
  Serial.print(" > ");
  Serial.print(V, DEC);
  Serial.print(" : 0x");
  Serial.print(V, HEX);
  Serial.print(" : 0b");
  Serial.println(V, BIN);
  set_zero();
}

void print_error(char *errmsg)  {
  /*  if (Serial.available())  {
   while (Serial.available())  {
   B = Serial.read();
   Serial.print(B, BYTE);
   }
   }
   */
  Serial.print(" *error* " );
  Serial.println(errmsg);
  set_zero();
  stateBits = 2; // garbage-in, look for a delimiter then start over
}

void  set_zero( void )  {
  V = 0;      // init, start looking for the next value
  charCount = 0;
  stateBits = 0;
}