在C#中将多位数转换为字母串的最佳方法是什么?
例如,如果我有一个数字说,
int digits = 1234567890
Run Code Online (Sandbox Code Playgroud)
我希望将其转换为字符串
string alpha = "ABCDEFGHIJ"
Run Code Online (Sandbox Code Playgroud)
这意味着1表示A,2表示B,依此类推.
像这样的东西:
int input = 123450;
string output = "";
while (input > 0)
{
int current = input % 10;
input /= 10;
if (current == 0)
current = 10;
output = (char)((char)'A' + (current - 1)) + output;
}
Console.WriteLine(output);
Run Code Online (Sandbox Code Playgroud)
上面的代码省去了通过数组或字典定义转换列表的麻烦.只需计算正确的Unicode代码点即可完成转换.