c# 字符转charCode

c#自带函数

//字符 -> charCode

int charCode = char.ConvertToUtf32(str, 0);

//charCode -> 字符

string ch = char.ConvertFromUtf32(charCode);

 

自己实现:字符 -> charCode

private static int GetCharCode(char c)
{
    var bytes = Encoding.Unicode.GetBytes(new char[] { c });
    int charCode = (int)bytes[1] << 8;
    charCode += (int)bytes[0];
    return charCode;
}

 

posted @ 2024-04-29 23:22  yanghui01  阅读(2)  评论(0编辑  收藏  举报