UInt32.TryParse() 十六进制数不起作用
由于某种原因,以下 C# 控制台程序始终输出:
32
错误
wtf=0
我做错了什么?
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Convert.ToUInt32("0x20", 16));
UInt32 wtf = 0;
Console.WriteLine(UInt32.TryParse("0x20",
NumberStyles.HexNumber, // I've tried also AllowHexSpecifier
CultureInfo.InvariantCulture, // I've also tried CurrentCulture
out wtf));
Console.WriteLine("wtf={0}", wtf);
}
}
}
For some reason the following C# Console program always outputs:
32
False
wtf=0
What am I doing wrong?
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Convert.ToUInt32("0x20", 16));
UInt32 wtf = 0;
Console.WriteLine(UInt32.TryParse("0x20",
NumberStyles.HexNumber, // I've tried also AllowHexSpecifier
CultureInfo.InvariantCulture, // I've also tried CurrentCulture
out wtf));
Console.WriteLine("wtf={0}", wtf);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要删除“0x”前缀。请参阅此博客条目
You need to drop the "0x" prefix. Please see this blog entry
去掉您要解析的字符串中的前导“0x”。
Get rid of the leading "0x" in the string you're trying to parse.
另请参阅 http://msdn.microsoft。 com/en-us/library/kadka85s%28v=VS.100%29.aspx
在页面底部的示例中:
See also http://msdn.microsoft.com/en-us/library/kadka85s%28v=VS.100%29.aspx
In the example at the bottom of the page: