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

本文关键字:转换 ascii 字符串 | 更新日期: 2023-09-27 18:19:22

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

my string "P0123456789"

谢谢。

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

可以使用

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

Output = GT

在这里找到了下面的方法

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;
}