C#:BigInteger 到指定基数的字符串
C#: BigInteger to string with radix specified
在 Java 和 Javascript 中,BigInteger
都有一个名为 toString(int radix)
其中 returns 这个 BigInteger
在给定的 radix
.
中的 String
表示
我想知道 C# (.NET) 中是否有任何方法可以完成 java 和java脚本能做什么?
您可以轻松地为此实现一个扩展方法,例如
using System.Linq;
using System.Numerics;
...
public static partial class BigIntegerExtensions {
// this have to be used for extend BigInteger
public static String ToRadixString(this BigInteger value, int radix) {
if (radix <= 1 || radix > 36)
throw new ArgumentOutOfRangeException(nameof(radix));
if (value == 0)
return "0";
bool negative = value < 0;
if (negative)
value = -value;
StringBuilder sb = new StringBuilder();
for (; value > 0; value /= radix) {
int d = (int)(value % radix);
sb.Append((char)(d < 10 ? '0' + d : 'A' - 10 + d));
}
return (negative ? "-" : "") + string.Concat(sb.ToString().Reverse());
}
}
然后使用它:
BigInteger x = 123456;
// HIID
Console.Write(x.ToRadixString(19));
演示:
BigInteger x = 123456;
string demo = string.Join(Environment.NewLine, Enumerable
.Range(2, 35)
.Select(radix => $"{x} (radix = {radix,2}) = {x.ToRadixString(radix)}"));
Console.Write(demo);
结果:
123456 (radix = 2) = 11110001001000000
123456 (radix = 3) = 20021100110
123456 (radix = 4) = 132021000
123456 (radix = 5) = 12422311
123456 (radix = 6) = 2351320
123456 (radix = 7) = 1022634
123456 (radix = 8) = 361100
123456 (radix = 9) = 207313
123456 (radix = 10) = 123456
123456 (radix = 11) = 84833
123456 (radix = 12) = 5B540
123456 (radix = 13) = 44268
123456 (radix = 14) = 32DC4
123456 (radix = 15) = 268A6
123456 (radix = 16) = 1E240
123456 (radix = 17) = 18232
123456 (radix = 18) = 1330C
123456 (radix = 19) = HIID
123456 (radix = 20) = F8CG
123456 (radix = 21) = D6JI
123456 (radix = 22) = BD1E
123456 (radix = 23) = A38F
123456 (radix = 24) = 8M80
123456 (radix = 25) = 7MD6
123456 (radix = 26) = 70G8
123456 (radix = 27) = 679C
123456 (radix = 28) = 5HD4
123456 (radix = 29) = 51N3
123456 (radix = 30) = 4H56
123456 (radix = 31) = 44EE
123456 (radix = 32) = 3OI0
123456 (radix = 33) = 3EC3
123456 (radix = 34) = 34R2
123456 (radix = 35) = 2URB
123456 (radix = 36) = 2N9C
在 Java 和 Javascript 中,BigInteger
都有一个名为 toString(int radix)
其中 returns 这个 BigInteger
在给定的 radix
.
String
表示
我想知道 C# (.NET) 中是否有任何方法可以完成 java 和java脚本能做什么?
您可以轻松地为此实现一个扩展方法,例如
using System.Linq;
using System.Numerics;
...
public static partial class BigIntegerExtensions {
// this have to be used for extend BigInteger
public static String ToRadixString(this BigInteger value, int radix) {
if (radix <= 1 || radix > 36)
throw new ArgumentOutOfRangeException(nameof(radix));
if (value == 0)
return "0";
bool negative = value < 0;
if (negative)
value = -value;
StringBuilder sb = new StringBuilder();
for (; value > 0; value /= radix) {
int d = (int)(value % radix);
sb.Append((char)(d < 10 ? '0' + d : 'A' - 10 + d));
}
return (negative ? "-" : "") + string.Concat(sb.ToString().Reverse());
}
}
然后使用它:
BigInteger x = 123456;
// HIID
Console.Write(x.ToRadixString(19));
演示:
BigInteger x = 123456;
string demo = string.Join(Environment.NewLine, Enumerable
.Range(2, 35)
.Select(radix => $"{x} (radix = {radix,2}) = {x.ToRadixString(radix)}"));
Console.Write(demo);
结果:
123456 (radix = 2) = 11110001001000000
123456 (radix = 3) = 20021100110
123456 (radix = 4) = 132021000
123456 (radix = 5) = 12422311
123456 (radix = 6) = 2351320
123456 (radix = 7) = 1022634
123456 (radix = 8) = 361100
123456 (radix = 9) = 207313
123456 (radix = 10) = 123456
123456 (radix = 11) = 84833
123456 (radix = 12) = 5B540
123456 (radix = 13) = 44268
123456 (radix = 14) = 32DC4
123456 (radix = 15) = 268A6
123456 (radix = 16) = 1E240
123456 (radix = 17) = 18232
123456 (radix = 18) = 1330C
123456 (radix = 19) = HIID
123456 (radix = 20) = F8CG
123456 (radix = 21) = D6JI
123456 (radix = 22) = BD1E
123456 (radix = 23) = A38F
123456 (radix = 24) = 8M80
123456 (radix = 25) = 7MD6
123456 (radix = 26) = 70G8
123456 (radix = 27) = 679C
123456 (radix = 28) = 5HD4
123456 (radix = 29) = 51N3
123456 (radix = 30) = 4H56
123456 (radix = 31) = 44EE
123456 (radix = 32) = 3OI0
123456 (radix = 33) = 3EC3
123456 (radix = 34) = 34R2
123456 (radix = 35) = 2URB
123456 (radix = 36) = 2N9C