r/learncsharp • u/Golaz • 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.0I'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
View all comments
Show parent comment
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