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.
Public Function BinToHex(ByVal BinStr As String) As String
Dim HexStr As String
HexStr = ""
Dim i As Integer
For i = 1 To Len(BinStr) Step 4
HexStr = HexStr & DecToHex(BinToDec(Mid(BinStr, i, 4)))
Next i
Return HexStr
End Function
In C # I have the following code:
public string BinToHex(string BinStr) {
string HexStr;
HexStr = "";
int i;
var loopTo = BinStr.Length;
for (i = 1; i <= loopTo; i +=4){
HexStr = HexStr + DecToHex(BinToDec(BinStr.Substring((i - 1), 4)));
}
return HexStr;
}
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: