【发布时间】:2011-09-05 03:28:01
【问题描述】:
我在 VS2005 上练习 StringToByteArray()。但是抛出异常。你能告诉我更多关于它的信息吗?
异常警报 **在 mscorlib.dll 中发生了“System.FormatException”类型的未处理异常
附加信息:找不到任何可识别的数字。**
public static byte[] StringToByteArray(String hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
// exception here
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
static void Main()
{
byte[] myByte = new byte[2];
myByte = StringToByteArray("0x0");
}
【问题讨论】: