ConvertBase C# Exercise

A silly function to convert a number (represented as a string) from one base to another. It can also help you count in base negative 2. (see negative bases) This should only be used for fun and experimentation because of all the wacky recursive calls and string manipulation.


 static void Main(string[] args)
    {
        Console.WriteLine(ConvertBase("1", 10, 2));
        //outputs 1
        Console.WriteLine(ConvertBase("2", 10, 2));
        //outputs 10
        Console.WriteLine(ConvertBase("3", 10, 2));
        //outputs 11

        Console.WriteLine(ConvertBase("1", 2, 10));
        //outputs 1
        Console.WriteLine(ConvertBase("10", 2, 10));
        //outputs 2
        Console.WriteLine(ConvertBase("11", 2, 10));
        //outputs 3

        Console.WriteLine(ConvertBase("10", 10, -2));
        //outputs 11110
        Console.WriteLine(ConvertBase("11", 10, -2));
        //outputs 11111
        Console.WriteLine(ConvertBase("12", 10, -2));
        //outputs 11100

        Console.WriteLine(ConvertBase("11110", -2, 10));
        //outputs 10
        Console.WriteLine(ConvertBase("11111", -2, 10));
        //outputs 11
        Console.WriteLine(ConvertBase("11100", -2, 10));
        //outputs 12

        Console.ReadLine();
    }

    public static string NegativeNumberToBaseString(int number, int baseNum)
    {
        var remainder = number % baseNum;
        int quotient = number / baseNum;

        if (remainder < 0)
        {
            remainder = remainder + Math.Abs(baseNum);
            quotient = quotient + 1;
        }

        var resultStorage = new List<string>();
        resultStorage.Add(remainder.ToString());

        while (quotient != 0)
        {
            remainder = quotient % baseNum;
            quotient = quotient / baseNum;

            if (remainder < 0)
            {
                remainder = remainder + Math.Abs(baseNum);
                quotient = quotient + 1;
            }

            resultStorage.Add(remainder.ToString());
        }

        resultStorage.Reverse();

        return String.Join("", resultStorage.ToArray());
    }

    public static string NumberToBaseString(string numberStr, int baseNum)
    {
        int number;

        if (!int.TryParse(numberStr, out number))
            return "";

        if (baseNum < 0)
        {
            return NegativeNumberToBaseString(number, baseNum);
        }

        if (number < baseNum)
        {
            return number.ToString();
        }

        var rem = number % baseNum;
        var result = rem.ToString();
        var reducedNum = (number - rem) / baseNum;
        var restOfString = NumberToBaseString(reducedNum.ToString(), baseNum);
        return restOfString + result;
    }

    public static int BaseStringToValue(string digitString, int baseNum)
    {
        if (string.IsNullOrEmpty(digitString))
            return 0;

        var result = digitString.Remove(0, digitString.Length - 1);
        var remainingString = digitString.Remove(digitString.Length - 1, 1);
        var valueOfRemainingString = BaseStringToValue(remainingString, baseNum);
        return int.Parse(result) + (baseNum * valueOfRemainingString);
    }

    public static string ConvertBase(string numberString, int baseOrginal, int baseResult)
    {
        if (baseOrginal == 0 || baseResult == 0)
            return string.Empty;

        var number = BaseStringToValue(numberString, baseOrginal);
        return NumberToBaseString(number.ToString(), baseResult);
    }
}


Leave a Reply

Your email address will not be published. Required fields are marked *