title: Issue with Substring function in C#
body: <p>I am implementing a number conversion program from Visual Basic.Net to C #. This is the original code that converts a number from binary to hexadecimal.</p>
  <pre><code>Public Function BinToHex(ByVal BinStr As String) As String 
     Dim HexStr As String 
     HexStr = &quot;&quot; 
     Dim i As Integer 
     For i = 1 To Len(BinStr) Step 4 
         HexStr = HexStr &amp; DecToHex(BinToDec(Mid(BinStr, i, 4))) 
     Next i 
     Return HexStr 
  End Function
  </code></pre>
  <p>In C # I have the following code:</p>
  <pre><code>public string BinToHex(string BinStr) {
      string HexStr;
      HexStr = &quot;&quot;;
      int i;
      var loopTo = BinStr.Length;
      for (i = 1; i &lt;= loopTo; i +=4){
          HexStr = HexStr + DecToHex(BinToDec(BinStr.Substring((i - 1), 4)));
      }
      return HexStr;
  }
  </code></pre>
  <p>This code makes use of the C#'s Substring function which, knowing that it is handled differently from Basic's mid function, we must subtract 1 from the variable that is used as the function's argument. But executing the function, for whatever value it receives, it throws the following error. The error poster is this:</p>
  <p><a href="https://i.stack.imgur.com/pCmuV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pCmuV.png" alt="Substring function error poster with invalid parameters" /></a></p>