简体   繁体   English

C#将大于Int64的数字转换为HexaDecimal

[英]C# Convert a number larger than Int64 to HexaDecimal

I am trying to convert a big number(ex: 9407524828459565063) to Hexadecimal(ex: 828E3DFD00000000) in C#. 我想在C#中将一个大数字(例如:9407524828459565063)转换为十六进制(例如:828E3DFD00000000)。

The problem is that the number is larger than Int64's max value. 问题是该数字大于Int64的最大值。

i looked up everywhere and couldn't find a working solution. 我到处查找,找不到合适的解决方案。

Any help over here? 有什么帮助吗?

Thank you. 谢谢。

I would use the System.Numerics.BigInteger class to do this. 我会使用System.Numerics.BigInteger类来执行此操作。 The exact solution depends on the format in which you have this number: string, double, other. 确切的解决方案取决于您具有此数字的格式:字符串,双精度,其他。

If string ( s ): 如果string( s ):

var bigInt = BigInteger.Parse(s);
var hexString = bigInt.ToString("x");

If double ( d ): 如果加倍( d ):

var bigInt = new BigInteger(d);
var hexString = bigInt.ToString("x");

... etcetera. ......等等。

Perhaps: 也许:

BigInteger b = 9407524828459565063;
var hex = b.ToString("X2");

Or 要么

ulong l = 9407524828459565063;
var hex = l.ToString("X2");

If you are using .NET 4.0, you might have a look at the BigInteger class: 如果您使用的是.NET 4.0,则可以查看BigInteger类:

http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx

  BigInteger bi = new BigInteger();
  bi = 9407524828459565063;
  string bix = bi.ToString("X");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM