十六进制到BigInteger转换

时间:2012-10-30 11:44:01

标签: c# hex biginteger

在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();
        }
    }
}

1 个答案:

答案 0 :(得分:3)

根据MSDN documentation idiom 只接受没有0x作为输入的十六进制字符串,但是通过输出带有前缀{{}的用户来欺骗用户1}}:

0x

这是一个非常垃圾的习语。我发明了适合你用例的自己的习语。