2

i'm trying the msdn example, using windows xp , .Net 4.0,

using System;
using System.Text;

class Example
{
   static void Main()
   {
      string unicodeString = "This string contains the unicode character Pi (\u03a0)";

      // Create two different encodings.
      Encoding ascii = Encoding.ASCII;
      Encoding unicode = Encoding.Unicode;

      // Convert the string into a byte array.
      byte[] unicodeBytes = unicode.GetBytes(unicodeString);

      // Perform the conversion from one encoding to the other.
      byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);

      // Convert the new byte[] into a char[] and then into a string.
      char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
      ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
      string asciiString = new string(asciiChars);

      // Display the strings created before and after the conversion.
      Console.WriteLine("Original string: {0}", unicodeString);
      Console.WriteLine("Ascii converted string: {0}", asciiString);
   }
}

Expected:

// The example displays the following output:
//    Original string: This string contains the unicode character Pi (Π)
//    Ascii converted string: This string contains the unicode character Pi (?)

But I'm getting

// The example displays the following output:
//    Original string: This string contains the unicode character Pi (?)
//    Ascii converted string: This string contains the unicode character Pi (?)
J-16 SDiZ
  • 26,473
  • 4
  • 65
  • 84
m4n07
  • 2,267
  • 12
  • 50
  • 65
  • http://stackoverflow.com/questions/1615559/converting-unicode-strings-to-escaped-ascii-string – Oskar Kjellin Aug 24 '11 at 07:21
  • @Oskar Kjellin it didn't work for me for some reason. – PythEch Aug 24 '11 at 07:32
  • 1
    setting the Console.OutputEncoding may solve the problem – PythEch Aug 24 '11 at 07:44
  • You may also need to set the console's font to a TrueType one. See http://stackoverflow.com/questions/5055659/c-unicode-string-output. For the reason why this is failing: http://stackoverflow.com/questions/3971684/c-console-font/3971750 – Ilian Aug 24 '11 at 07:56
  • Also you need to fix/check HKEY_CURRENT_USER\Console\Codepage or delete that. For example using ComboFix makes that value ASCII so the unicode chars cannot be seen. – PythEch Aug 24 '11 at 07:59
  • I'm trying to have a label and print the unicode on that label. In that case what changes you suggest ? – m4n07 Aug 24 '11 at 08:06
  • label1.Text = "This string contains the unicode character Pi (π)"; works fine. You can change label's font if you want a more understandable symbol. – PythEch Aug 24 '11 at 08:08
  • Has the problem been solved? Give us a feedback please. – PythEch Aug 24 '11 at 08:16
  • If i try something like label1.Text ="This string contains the unicode character of triangle (◄)"; IT doesnt work. – m4n07 Aug 24 '11 at 08:22
  • It works for me but it's weird. I suggest using Arial (Normal one) font family and it can be done easily in design mode. Also you can set the text on there. – PythEch Aug 24 '11 at 08:38
  • Yes I worked around by selecting a unicode font. – m4n07 Aug 24 '11 at 08:39
  • The default font Microsoft Sans Serif works too but if you're using another font it may cause that problem. – PythEch Aug 24 '11 at 08:40
  • Default font doesnt work for me .It works only with Ms sans serif unicode – m4n07 Aug 24 '11 at 09:19

0 Answers0