Quickest way to convert a base 10 number to any base in .NET?

后端 未结 12 1223
予麋鹿
予麋鹿 2020-11-22 04:07

I have and old(ish) C# method I wrote that takes a number and converts it to any base:

string ConvertToBase(int number, char[] baseChars);

12条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 04:28

    Could this class from this forum post help you?

    public class BaseConverter { 
    
    public static string ToBase(string number, int start_base, int target_base) { 
    
      int base10 = this.ToBase10(number, start_base); 
      string rtn = this.FromBase10(base10, target_base); 
      return rtn; 
    
    } 
    
    public static int ToBase10(string number, int start_base) { 
    
      if (start_base < 2 || start_base > 36) return 0; 
      if (start_base == 10) return Convert.ToInt32(number); 
    
      char[] chrs = number.ToCharArray(); 
      int m = chrs.Length - 1; 
      int n = start_base; 
      int x; 
      int rtn = 0; 
    
      foreach(char c in chrs) { 
    
        if (char.IsNumber(c)) 
          x = int.Parse(c.ToString()); 
        else 
          x = Convert.ToInt32(c) - 55; 
    
        rtn += x * (Convert.ToInt32(Math.Pow(n, m))); 
    
        m--; 
    
      } 
    
      return rtn; 
    
    } 
    
    public static string FromBase10(int number, int target_base) { 
    
      if (target_base < 2 || target_base > 36) return ""; 
      if (target_base == 10) return number.ToString(); 
    
      int n = target_base; 
      int q = number; 
      int r; 
      string rtn = ""; 
    
      while (q >= n) { 
    
        r = q % n; 
        q = q / n; 
    
        if (r < 10) 
          rtn = r.ToString() + rtn; 
        else 
          rtn = Convert.ToChar(r + 55).ToString() + rtn; 
    
      } 
    
      if (q < 10) 
        rtn = q.ToString() + rtn; 
      else 
        rtn = Convert.ToChar(q + 55).ToString() + rtn; 
    
      return rtn; 
    
    } 
    
    }
    

    Totally untested... let me know if it works! (Copy-pasted it in case the forum post goes away or something...)

提交回复
热议问题