base_convert in .NET

元气小坏坏 提交于 2019-12-04 08:26:24
Erik Forbes

Here's some code that'll convert an integer to an arbitrary base up to 36, and convert a string representation of a base x value to an integer (given the base):

class Program {
    static void Main(string[] args) {
        int b10 = 123;
        int targetBase = 5;

        string converted = ConvertToBase(b10, targetBase);
        int convertedBack = ConvertFromBase(converted, targetBase);

        string base3 = "212210";
        string base7 = ConvertFromBaseToBase(base3, 3, 7);

        Console.WriteLine(converted);
        Console.WriteLine(convertedBack);
        Console.WriteLine(base7);
        Console.ReadLine();
    }

    private const string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static string ConvertToBase(int b10, int targetBase) {
        if (targetBase < 2) throw new ArgumentException("Target base must be greater than 2.", "targetBase");
        if (targetBase > 36) throw new ArgumentException("Target base must be less than 36.", "targetBase");

        if (targetBase == 10) return b10.ToString();

        StringBuilder result = new StringBuilder();

        while (b10 >= targetBase) {
            int mod = b10 % targetBase;
            result.Append(chars[mod]);
            b10 = b10 / targetBase;
        }

        result.Append(chars[b10]);

        return Reverse(result.ToString());
    }

    private static int ConvertFromBase(string bx, int fromBase) {
        if (fromBase < 2) throw new ArgumentException("Base must be greater than 2.", "fromBase");
        if (fromBase > 36) throw new ArgumentException("Base must be less than 36.", "fromBase");

        if (fromBase == 10) return int.Parse(bx);

        bx = Reverse(bx);
        int acc = 0;

        for (int i = 0; i < bx.Length; i++) {
            int charValue = chars.IndexOf(bx[i]);
            acc += (int)Math.Pow(fromBase, i) * charValue;
        }

        return acc;
    }

    public static string ConvertFromBaseToBase(string bx, int fromBase, int toBase) {
        int b10 = ConvertFromBase(bx, fromBase);
        return ConvertToBase(b10, toBase);
    }

    public static string Reverse(string s) {
        char[] charArray = new char[s.Length];
        int len = s.Length - 1;
        for (int i = 0; i <= len; i++)
            charArray[i] = s[len - i];
        return new string(charArray);
    }
}

If you're unconcerned with displaying these values, you can use extended characters in your chars set - if you stick to plain ascii, you can theoretically have base256 values. Going beyond that I would recommend not using chars, but instead using some other uniquely-identifiable value - though I don't much see the value.

EDIT: This answer is very convenient, but only works for bases 2, 8, 10 and 16

You can use Convert.ToInt32(text, base) and then Convert.ToString(number, base):

using System;

class Test
{
    static void Main()
    {
        int number = Convert.ToInt32("101", 2);
        string text = Convert.ToString(number, 10);
        Console.WriteLine(text); // Prints 5
    }
}

If you're converting to or from base 10, you don't need to specify that - it's the default.

Note that this only works for bases 2, 8, 10 and 16. If you want anything else, you'll have to write your own parser/formatter.

In ConvertToBase, the following line:

while (b10 > targetBase)

...should be:

while (b10 >= targetBase)

Which takes care of the base number popping up in the converted number (e.g. converting "3" into base 3 yields "3" instead of "10").

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!