【发布时间】:2013-02-18 13:33:49
【问题描述】:
我需要将 BigInteger 打印为负数,但是 ToString("X") 的十六进制重载不正确。
BigInteger be1 = new BigInteger();
be1 = 0x7e;
Console.WriteLine(be1.ToString()); // 126
Console.WriteLine(be1.ToString("X")); // 7E
Console.WriteLine(be1.ToString("x")); // 7e
Console.WriteLine();
be1 = BigInteger.Negate(be1);
Console.WriteLine(be1.ToString()); // -126 OK
Console.WriteLine(be1.ToString("X")); // 82 WRONG
Console.WriteLine(be1.ToString("x")); // 82 WRONG
我做错了什么,我该如何解决这个问题?
(我这样做是值得的,所以我可以match the hex output here, illustrated as an C++ array)
【问题讨论】:
-
假设一个 8 位数字并没有错。 -1 = 0xff,-128 = 0x80,-127 = 0x81,-126 如图所示
标签: c# .net tostring biginteger