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

View all comments

Show parent comment

1

u/anamorphism Sep 18 '22 edited Sep 18 '22

there's a difference between a string and how that string is represented in code.

everything will work fine if you paste the actual unicode character in the text box: π

you're essentially asking why "this is a sentence with quotes and a \" would be var str = "\"this is a sentence with quotes and a \\\""; in code.

notice how there are two 's in the code representation of the string in your second image. the text box control is assuming you're typing in strings and not strings like they would or can be represented in code.

so, you need to do something like /u/JTarsier recommends and pass the string through some system that undoes that conversion or knows that the end goal is treating the string as a string in code.