在 C# 中将整数转换为字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/289792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:50:08  来源:igfitidea点击:

Int to Char in C#

c#casting

提问by Boaz

What is the best way to convert an Int value to the corresponding Char in Utf16, given that the Int is in the range of valid values?

鉴于 Int 在有效值的范围内,将 Int 值转换为 Utf16 中相应 Char 的最佳方法是什么?

采纳答案by gimel

(char)myint;

for example:

例如:

Console.WriteLine("(char)122 is {0}", (char)122);

yields:

产量:

(char)122 is z

(char)122 是 z

回答by Corey Trager

int i = 65;
char c = Convert.ToChar(i);

回答by Boaz

Although not exactly answering the question as formulated, but if you need or can take the end result as string you can also use

虽然没有完全按照公式回答问题,但是如果您需要或可以将最终结果作为字符串,您也可以使用

string s = Char.ConvertFromUtf32(56);

which will give you surrogate UTF-16 pairs if needed, protecting you if you are out side of the BMP.

如果需要,它将为您提供代理 UTF-16 对,如果您不在BMP范围内,则可以保护您。