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

后端 未结 12 1463
予麋鹿
予麋鹿 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:22

    One can also use slightly modified version of the accepted one and adjust base characters string to it's needs:

    public static string Int32ToString(int value, int toBase)
    {
        string result = string.Empty;
        do
        {
            result = "0123456789ABCDEF"[value % toBase] + result;
            value /= toBase;
        }
        while (value > 0);
    
        return result;
    }
    

提交回复
热议问题