如何在C#中将字符串的ascii值之和转换为base 36

时间:2013-06-21 22:09:02

标签: c# base36

在C#中,如何将字符串的ascii值之和转换为基数36?

我的字符串“P0123456789”

感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用

var s = "P0123456789";
var result = s.Sum(x => x);
var base36ed = ConvertToBase(result,36);
  

输出= GT

找到以下方法here

public String ConvertToBase(int num, int nbase)
{
String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

// check if we can convert to another base
if(nbase < 2 || nbase > chars.Length)
    return "";

int r;
String newNumber = "";

// in r we have the offset of the char that was converted to the new base
while(num >= nbase)
{
    r = num % nbase;
    newNumber = chars[r] + newNumber;
    num = num / nbase;
}
// the last number to convert
newNumber = chars[num] + newNumber;

return newNumber;
}