【发布时间】:2019-10-07 13:31:50
【问题描述】:
我正在尝试检查字符串是否包含有效的十六进制数。
我正在使用Check if string is valid represantion of HEX number 中记录的方法。
使用以下代码:
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
int temp;
bool b = int.TryParse("0x5", System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out temp);
System.Console.Write("{0} : {1}", b, temp);
}
}
我在输出端得到:False : 0。
这里有什么问题?
【问题讨论】:
-
它似乎无法解析“0x”部分,如果你只写例如 int.TryParse("5"...) 那么它可以工作(也 int.TryParse("A "....) 有效)。
标签: c#