【发布时间】:2012-10-19 17:59:30
【问题描述】:
在 C# 中将标准十六进制字符串(如“0x0123”)转换为 BigInteger 的惯用方法是什么?
我尝试的需要手动删除十六进制前缀:
using System;
using System.Numerics;
using System.Globalization;
namespace TestHex
{
class Program
{
static void Main(string[] args)
{
BigInteger A;
// it does not work
// A = BigInteger.Parse("0x0123");
// it works, but without hex prefix
A = BigInteger.Parse("123", NumberStyles.AllowHexSpecifier);
Console.WriteLine(A);
Console.ReadLine();
}
}
}
【问题讨论】:
-
如果适合长,可以使用
var A = new BigInteger(Convert.ToInt64("0x123", 16)); -
嗯。对于那个枚举值来说,这是一个真的糟糕的名字。 “我们想要一个将数字解析为十六进制的值,但不允许他们将其指定为带有标准十六进制前缀的十六进制。我们称之为 AllowHexSpecifier!”
-
您的问题不清楚,请澄清您要做什么。我除了一个拥有 15.9K 声望点的人提出的更详细的问题。整个以身作则的领导角色。
标签: c# hex biginteger