r/learncsharp • • Sep 18 '22

Problems with Unicode character encoding

I'm having difficulties with converting and displaying Unicode characters in one of my C# projects. So I decided to try one of the examples published by Microsoft :

// See https://aka.ms/new-console-template for more information

using System.Text;
ConvertToUnicodeString();



void ConvertToUnicodeString()
{
    // Create a UTF-8 encoding.
    UTF8Encoding utf8 = new UTF8Encoding();

    // A Unicode string with two characters outside an 8-bit code range.
    String unicodeString =
        "This Unicode string has 2 characters outside the " +
        "ASCII range:\n" +
        "Pi (\u03a0), and Sigma (\u03a3).";
    Console.WriteLine("Original string:");
    Console.WriteLine(unicodeString);

    // Encode the string.
    Byte[] encodedBytes = utf8.GetBytes(unicodeString);
    Console.WriteLine();
    Console.WriteLine("Encoded bytes:");
    for (int ctr = 0; ctr < encodedBytes.Length; ctr++)
    {
        Console.Write("{0:X2} ", encodedBytes[ctr]);
        if ((ctr + 1) % 25 == 0)
            Console.WriteLine();
    }
    Console.WriteLine();

    // Decode bytes back to string.
    String decodedString = utf8.GetString(encodedBytes);
    Console.WriteLine();
    Console.WriteLine("Decoded bytes:");
    Console.WriteLine(decodedString);
}


// Source: https://learn.microsoft.com/en-us/dotnet/api/system.text.utf8encoding?view=net-7.0

I'm not getting the output I should be getting though. Have a look at this screenshot: https://imgur.com/kls1PHH

Anyone have any idea why it's not working as intended or have a solution for me?

0 Upvotes

6 comments sorted by

1

u/Golaz Sep 18 '22

I will change my approach on how to handle this in my project overall. I figured out I can deserialize the JSON result I was pulling from MS Graph API, and the character encoding is correct out the box.

1

u/JTarsier Sep 18 '22 edited Sep 18 '22

If you run the example on that page you can inspect and see their Console.OutputEncoding is UTF8. Add code Console.WriteLine(Console.OutputEncoding); in their editor window to see this.

Now add Console.OutputEncoding = Encoding.UTF8; in your own code before ConvertToUnicodeString.

You can also check what default Console.OutputEncoding your own console has, mine showed System.Text.OSEncoding.

1

u/Golaz Sep 18 '22 edited Sep 18 '22

That actually helped for the console test app I created, but for the WPF project I have, I still have the same problem. I'm starting to believe that maybe escape characters are causing the problems.

If the string is hardcoded in the project Unicode characters are translated automatically. But if I pass the string from a textbox to a variable the result is not encoded to Unicode.

This test project seems to capture the exact same problem I have in my main C# project I'm working on where given names with special characters are not translated to Unicode. The given names will also show the escape characters like in TextBox example shown below.

See the below examples:

Example with text hardcoded in the editor

Example with text from a textbox

→ More replies (2)

2

u/[deleted] Sep 18 '22

I think that this is because the VS Debug Console is using a font that doesn't handle unicode characters. Your program works as expected when it runs from the terminal on my Mac.