I'm trying to write down a UTF-8 string (Vietnamese) into C# Console but no success. I'm running on Windows 7.
I tried to use the Encoding
class that convert string
to char[]
to byte[]
and then to String, but no help, the string is input directly from the database.
Here is some example
Tôi tên là Đức, cuộc sống thật vui vẻ tuyệt vời
It does not show the special character like Đ
or ứ
... instead it show up ?
, much worse than with the Encoding class.
Does anyone can try this out or know about this problem?
My code
static void Main(string[] args)
{
XDataContext _new = new XDataContext();
Console.OutputEncoding = Encoding.GetEncoding("UTF-8");
string srcString = _new.Posts.First().TITLE;
Console.WriteLine(srcString);
// Convert the UTF-16 encoded source string to UTF-8 and ASCII.
byte[] utf8String = Encoding.UTF8.GetBytes(srcString);
byte[] asciiString = Encoding.ASCII.GetBytes(srcString);
// Write the UTF-8 and ASCII encoded byte arrays.
Console.WriteLine("UTF-8 Bytes: {0}", BitConverter.ToString(utf8String));
Console.WriteLine("ASCII Bytes: {0}", BitConverter.ToString(asciiString));
// Convert UTF-8 and ASCII encoded bytes back to UTF-16 encoded
// string and write.
Console.WriteLine("UTF-8 Text : {0}", Encoding.UTF8.GetString(utf8String));
Console.WriteLine("ASCII Text : {0}", Encoding.ASCII.GetString(asciiString));
Console.WriteLine(Encoding.UTF8.GetString(utf8String));
Console.WriteLine(Encoding.ASCII.GetString(asciiString));
}
and here is the outstanding output
Nhà báo đi hội báo Xuân
UTF-8 Bytes: 4E-68-C3-A0-20-62-C3-A1-6F-20-C4-91-69-20-68-E1-BB-99-69-20-62-C3-
A1-6F-20-58-75-C3-A2-6E
ASCII Bytes: 4E-68-3F-20-62-3F-6F-20-3F-69-20-68-3F-69-20-62-3F-6F-20-58-75-3F-
6E
UTF-8 Text : Nhà báo đi hội báo Xuân
ASCII Text : Nh? b?o ?i h?i b?o Xu?n
Nhà báo đi hội báo Xuân
Nh? b?o ?i h?i b?o Xu?n
Press any key to continue . . .
You will need to set
Console.OutputEncoding
to matchUTF-8
.Probably something like:
it is the problem with cmd.exe console. It doesn't support unicode. [Nothing to do with C#/.NET]
Try changing it to a GUI app if you can or write to a file.
Does the font you use in the Console window support the characters you are trying to display?
Screenshot of the output (use Consolas or another font that has all the above characters):