BigInteger的解析至八进制字符串转换? [英] BigInteger Parse Octal String?
问题描述
在Java中,我能做到
In Java, I could do
//Parsing Octal String
BigInteger b = new BigInteger("16304103460644701340432043410021040424210140423204",8);
接着,当我高兴格式化
Then format it as I pleased
b.toString(2); //2 for binary
b.toString(10); //10 for decimal
b.toString(16); //16 for hexadecimal
C#的的BigInteger
提供上面显示的格式设置功能,但我似乎无法找到一种方法来解析BIIIG(大于64位无符号)八进制的值。
C#'s BigInteger
offers the formatting capabilities shown above but I can't seem to find a way to parse BIIIG (greater than 64 bit, unsigned) Octal values.
推荐答案
这可能不是最有效的解决方案,但如果表现不是很重要,您可以构造的BigInteger
手动:
This may not be the most efficient solution, but if performance is not a priority, you can construct the BigInteger
manually:
string s = "16304103460644701340432043410021040424210140423204";
BigInteger bi = s.Aggregate(new BigInteger(), (b, c) => b * 8 + c - '0');
上面的解决方案还适用于任何碱不大于10;只需更换 8
在上面的code。与您所需的基准。
The above solution also works for any base not greater than 10; just replace the 8
in the above code with your required base.
修改:对于十六进制数字,你应该使用解析
方法。 prePEND与 0
如果您的号码应该是PTED因为即使它的第一个字符是正间$ P $ 8
- ˚F
Edit: For hexadecimal numbers, you should use the Parse
method. Prepend with 0
if your number should be interpreted as positive even if its first character is 8
–F
.
string s = "0F20051C5E45F4FD68F8E58905A133BCA";
BigInteger bi = BigInteger.Parse(s, NumberStyles.HexNumber);
这篇关于BigInteger的解析至八进制字符串转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!