如何打印utf8编码的字符? [英] How can i print an utf8 encoded character?
问题描述
你好,我在这里需要帮助.我有一个字符串,该字符串与十六进制格式的utf8编码字符相对应.例如:
Hello, i need a help here. I have a string which corresponds to an utf8 encoded character in its hexadecimal form. for example:
String s="c2b5";
哪个字符是
µ
在unicode表中以其十六进制形式显示.谁能告诉我如何在控制台或文件中打印此字符?
谢谢.
更新:
这不起作用,编译器错误.
Which is the character
µ
in the unicode table in its hex form. Can anyone tell me how can i print this char in a console or a file?
Thank you.
UPDATE:
This doesn''t work, compiler error.
String mystr = "\u"+"03a3";
Byte[] myb = utf8.GetBytes(mystr);
String dec = utf8.GetString(myb);
Console.WriteLine(dec);
虽然这行得通
Although this works
String mystr = "\u03a3";
Byte[] myb = utf8.GetBytes(mystr);
String dec = utf8.GetString(myb);
Console.WriteLine(dec);
推荐答案
不!希腊文的µ是"03BC"!这是:
No! The Greek µ is "03BC"! This is:
char ch = '\x03bc';
//or
ch = '\u03bc';
//or
const string MuString = "03BC";
ushort utf16 = ushort.Parse(MuString, System.Globalization.NumberStyles.HexNumber);
ch = (char)utf16;
现在,您可以将其写入文件,但是请确保使用Unicode UTF格式之一的编码来创建文件.例如:
Now, you can write it into the file, but make sure the file is created with the encoding of one of Unicode UTF formats. For example:
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fileName, true, System.Text.UTF8Encoding.UTF8)) {
//...
writer.Write(ch);
//...
}
请参阅 http://msdn.microsoft.com/en-us/library/system.io .streamwriter.aspx [ ^ ].
See http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx[^].
这篇关于如何打印utf8编码的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!