Hex to int C# with VERY big numbers

邮差的信 提交于 2019-12-21 07:05:27

问题


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.


回答1:


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



回答2:


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)



来源:https://stackoverflow.com/questions/6498288/hex-to-int-c-sharp-with-very-big-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!