Hex to int C# with VERY big numbers

2019-04-03 23:57发布

I have a 256 chars long string that contains a hex value:

EC851A69B8ACD843164E10CFF70CF9E86DC2FEE3CF6F374B43C854E3342A2F1AC3E30C741CC41E679DF6D07CE6FA3A66083EC9B8C8BF3AF05D8BDBB0AA6CB3EF8C5BAA2A5E531BA9E28592F99E0FE4F95169A6C63F635D0197E325C5EC76219B907E4EBDCD401FB1986E4E3CA661FF73E7E2B8FD9988E753B7042B2BBCA76679

I want to convert it to a string with numbers like this:

102721434037452409244947064576168505707480035762262934026941212181473785225928879178124028140134582697986427982801088884553965371786856967476796072433168989860379885762727214550528198941038582937788145880903882293999022181847665735412629158069472562567144696160522107094738216218810820997773451212693036210879

How can it be done with so big numbers?

Thanks in advance.

2条回答
贪生不怕死
2楼-- · 2019-04-04 00:19

Use BigInteger. Specifically, you can use BigInteger.Parse to parse the hexadecimal representation to an instance of BigInteger (use NumberStyles.HexNumber), and then BigInteger.ToString to get the decimal representation.

var number = BigInteger.Parse(
    "EC851A69B8ACD843164E10CFF70CF9E86DC2FEE3CF6F374B43C854E3342A2F1AC3E30C741CC41E679DF6D07CE6FA3A66083EC9B8C8BF3AF05D8BDBB0AA6CB3EF8C5BAA2A5E531BA9E28592F99E0FE4F95169A6C63F635D0197E325C5EC76219B907E4EBDCD401FB1986E4E3CA661FF73E7E2B8FD9988E753B7042B2BBCA76679",
    NumberStyles.HexNumber
);
var s = number.ToString();
查看更多
成全新的幸福
3楼-- · 2019-04-04 00:38

Use the System.Numerics.BigInteger to store the number. To obtain it use BigInteger.Parse with a NumberFlags value where the AllowHexSpecifier is set. (Such as NumberFlags.HexNumber)

查看更多
登录 后发表回答