Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

C#: BigInteger to string with radix specified

In both Java and Javascript, BigInteger have a function called toString(int radix) which returns the String representation of this BigInteger in the given radix.

I'm wondering is there any approach in C# (.NET) to do the job that java and javascript can do?

question from:https://stackoverflow.com/questions/65943008/c-biginteger-to-string-with-radix-specified

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can easily implement an extension method for this, e.g.

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

And then use it:

 BigInteger x = 123456;

 // HIID
 Console.Write(x.ToRadixString(19));

Demo:

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

Outcome:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...