title: Pascal to C# conversion
body: <p>I am trying to convert this pascal code into C# in order to communicate with a peripheral device attached to a comm port. This piece of code should calculate the Control Byte, however I'm not getting the right hex Value therefore I'm wondering if I'm converting the code in the right way.
  Pascal:</p>
  
  <pre><code>begin
    check := 255;
    for i:= 3 to length(sequence)-4 do
    check := check xor byte(sequence[i]);
  end;
  </code></pre>
  
  <p>C#:</p>
  
  <pre><code>int check = 255;
  for (int x = 3; x &lt; (sequence.Length - 4); x++)
  {
      check = check ^ (byte)(sequence[x]);
  }
  </code></pre>
  
  <p>Pascal function:</p>
  
  <pre><code>{ *** conversion of number into  string ‘hex’ *** }
  function word_to_hex (w: word) : string;
    var
    i : integer;
    s : string;
    b : byte;
    c : char;
  begin
    s := ‘’;
    for i:= 0 to 3 do
    begin
      b := (hi(w) shr 4) and 15;
      case b of
      0..9 : c := char(b+$30);
      10..15 : c := char(b+$41-10);
    end;
    s := s + c;
    w := w shl 4;
  end;
  word_ to_hex := s;
  end;
  </code></pre>
  
  <p>C# Equivalent:</p>
  
  <pre><code>     public string ControlByte(string check)
      {
          string s = "";
          byte b;
          char c = '\0';
          //shift = check &gt;&gt; 4 &amp; 15;
          for (int x = 0; x &lt;= 3; x++)
          {
              b = (byte)((Convert.ToInt32(check) &gt;&gt; 4) &amp; 15);
  
              if (b &gt;= 0 &amp;&amp; b &lt;= 9)
              {
                  c = (char)(b + 0x30);
              }
              else if (b &gt;= 10 &amp;&amp; b &lt;= 15)
              {
                  c = (char)(b + 0x41 - 10);
              }
              s = s + c;
              check = (Convert.ToInt32(check) &lt;&lt; 4).ToString();
          }
          return s;
      }
  </code></pre>
  
  <p>And last pascal:</p>
  
  <pre><code>function byte_to_hex (b:byte) : string;
  begin
  byte_to_hex := copy(word_to_hex(word(b)),3,2);
  end;
  </code></pre>
  
  <p>which i am not sure how is substringing the result from the function. So please let me know if there is something wrong with the code conversion and whether I need to convert the function result into bytes. I appreciate your help, UF.</p>
  
  <p>Further info EDIT: Initially I send a string sequence containing the command and information that printer is supposed to print. Since every sequence has a unique Control Byte (in Hex) I have to calculate this from the sequence (sequence = "P1;1$l201PrinterPrinterPrinter1B/100.00/100.00/0/\") which is what upper code does according to POSNET=>"cc – control byte, encoded as 2 HEX digits (EXOR of all characters after ESC P to this byte with  #255 initial quantity), according to the following algorithm in PASCAL language:(see first code block)".=>1.   check number calculated in the above loop which constitutes control byte should be recoded into two HEX characters (ASCII characters from scope: ‘0’..’9’,’A’..’F’,’a’..’f’),  utilizing the following  byte_to_hex function:(see third code block). =>{<strong>*  conversion of byte into 2 characters *</strong>}(see 5th code block)</p>