所以我正在将一些代码从 VB.NET 迁移到 C#,但是当它在 C# 中进行字节解析时它失败了。
这里是 VB.NET 代码可以工作:
Dim sModifiedAccountNumber_AsciiHex
Dim iByte As Byte = 0
Dim iIdx As Integer = 0
Dim strByte As String = String.Empty
sModifiedAccountNumber_AsciiHex = "FC13"
For iIdx = 1 To 3 Step 2
iByte = CByte("&H" & Mid$(sModifiedAccountNumber_AsciiHex, iIdx, 2))
If iByte >= 120 And iByte <= 127 Then
iByte = iByte Or &H80
strByte = Hex$(iByte)
Do While Len(strByte) < 2
strByte = "0" & strByte
Loop
Mid$(sModifiedAccountNumber_AsciiHex, iIdx, 2) = strByte
End If
Next
C# 版本:
string modAccountNumberAsciiHex = "FC13";
byte iByte;
string strByte = string.Empty;
for (int iIdx = 1; iIdx <= 3; iIdx += 2)
{
iByte = byte.Parse(("&H" + modAccountNumberAsciiHex.Substring((iIdx - 1), 2)));
if (iByte >= 120 && iByte <= 127)
{
iByte = iByte |= 0x80;
strByte = BitConverter.ToString(new byte[] { iByte });
while (strByte.Length < 2)
{
strByte = "0" + strByte;
}
// TODO: convert the line below to C#
// Mid$(sModifiedAccountNumber_AsciiHex, iIdx, 2) = strByte
}
}
所以在 C# 中我总是FormatException
在执行byte.Parse
(for 语句之后的行)时得到一个
关于这在 C# 中应该是什么的任何想法?
此外 - TODO 注释中的 C# 版本也将不胜感激:-)