title: Decode and reformat values from Serial data array
body: <p>My application receives data from a serial interface. Some received records can displayed directly to a textbox, others have to be converted. One Serial Messages that i have to decode contains 512byte and multiple informations.</p>
  <p>To receive Data i use:</p>
  <pre><code>    private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
      {
          var serialPort = (SerialPort)sender;
          string data = serialPort.ReadExisting();
          dataDecoder(data);         
      }
  </code></pre>
  <p>And in the data decoder i do the following:</p>
  <pre><code>        private void dataDecoder(string input)
      {
          string post = &quot;&quot;;                       //empty string
          char[] buf = input.ToCharArray();       //Converter input String to Char Array
  
          if (buf[0] == '#')                       //only process records starting with #, others go directly to SetText(string)
          {
              post += &quot;Decode, Len=&quot;;                                      //Informal message   
              int up = int.Parse(buf[2].ToString());                       //Upper byte of 16bit-binary integer
              int low = int.Parse(buf[3].ToString());                      //lower byte of 16bit-binary integer
              int len = low &lt;&lt; 8 | up;                                     //converter to decimal int
              post += len;                                                 //edit string
              post += &quot; Channel: &quot;;                                        //informal message
              int first = int.Parse(buf[4].ToString());                    //Upper byte of 16bit-binary integer
              int second = int.Parse(buf[5].ToString());                   //lower byte of 16bit-binary integer
              int chnl = second &lt;&lt; 8 | first;                              //converter to decimal int
              post += chnl;                                                //edit string
              post += &quot;DATA&quot;;                                              //informal message
  
  
              for (int i = 0; i &lt; 507; i++)                                  //decode contents 6 to 511 (512byte is checsum)
              {
                  int k = i + 6;                                           //format selector
                  int one = int.Parse(buf[k].ToString());                  //Highest byte of 32bit-binary integer
                  int two = int.Parse(buf[k + 1].ToString());                //second high byte of 32bit-binary integer
                  int three = int.Parse(buf[k + 2].ToString());            //second low byte of 32bit-binary integer
                  int four = int.Parse(buf[k + 3].ToString());             //lowest byte of 32bit-binary integer
                  int rec = four &lt;&lt; 24 | three &lt;&lt; 16 | two &lt;&lt; 8 | one;     //convert to decimal int
                  post += rec;                                             //edit string                  
                  post += &quot;,&quot;;                                             //separation character
              }
              SetText(post);                                               //set text to texbox
          }
          else
          {
              SetText(input);                                              //set input to textbox (unconverted)
          }
      }
  }
  </code></pre>
  <p>Build does not report any errors or warnings. But when i run the code and it reaches this function, the program does crash with the message</p>
  <blockquote>
  <p>System.FormatException: 'Input string was not in a correct format.'</p>
  </blockquote>
  <p>on the line</p>
  <pre><code>int up = int.Parse(buf[2].ToString());
  </code></pre>
  <p>What could i do better to solve this issue?</p>
  <p><strong>EDIT</strong>
  Here is the first parts of the Serial data (taken from Serial Monitor)</p>
  <blockquote>
  <p>23 42 FC 01 00 00 00 00 00 00 00 00 00 00 00 00</p>
  </blockquote>
  <p>23 is #
  42 is B
  and FC 01 is the record lenght, in this case is known, and its 508 (01FC)</p>