locked
Convert string to Unicode RRS feed

  • Question

  • How can I convert a string to Unicode?

    The output should be the Unicode string.

     

    Noam B.

    Tuesday, June 15, 2010 11:39 AM

Answers

  • Hi Noam,

    Try this:

    using System;
    using System.Text;
    
    namespace ConsoleApplication3
    {
      class Program
      {
        static void Main(string[] args)
        {
          string inputString = new string(new char[] { '\u70B9', '\u83DC' }); // 点菜
           byte[] stringBytes = Encoding.Unicode.GetBytes(inputString);
          char[] stringChars = Encoding.Unicode.GetChars(stringBytes);
          StringBuilder builder = new StringBuilder();
          Array.ForEach<char>(stringChars, c => builder.AppendFormat("\\u{0:X}", (int) c));
          Console.WriteLine(builder);
    
          Console.ReadKey(true);
        }
      }
    }
    

     

    Marcel

    • Marked as answer by Noam B Tuesday, June 15, 2010 1:12 PM
    Tuesday, June 15, 2010 12:47 PM
  • Well, later I found a very easy way to do that, so for those of you that will search for a solution,

    here is a way to imitate the JavaScript escape command:

    Add a reference to Microsoft.JScript.dll

     

    string result = Microsoft.JScript.GlobalObject.escape(inputString);

     

    Noam B
    _________________________________________________________

    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Marked as answer by Noam B Thursday, June 17, 2010 12:31 PM
    Thursday, June 17, 2010 12:30 PM

All replies

  • Could you give an example of what you want? C# strings are already Unicode.

    Tuesday, June 15, 2010 12:03 PM
  •  string str = "Æble";   
       byte [] unibyte=Encoding.Unicode.GetBytes(str);
       string uniString = string.Empty;
       foreach (byte b in unibyte)
       {
       uniString += string.Format("{0}{1}", @"\u", b.ToString("X"));
    
       }

    is it what you wanted


    Manish Sati

     

    • Edited by Manish Sati Tuesday, June 15, 2010 12:35 PM
    Tuesday, June 15, 2010 12:16 PM
  • Almost.

    I need the \u at the beginning of each series also...

    Tuesday, June 15, 2010 12:19 PM
  • uniString += string.Format("{0}{1}", @"\u", b.ToString("X"));
    

     


    Manish Sati

    Tuesday, June 15, 2010 12:32 PM
  • Hi Noam,

    Try this:

    using System;
    using System.Text;
    
    namespace ConsoleApplication3
    {
      class Program
      {
        static void Main(string[] args)
        {
          string inputString = new string(new char[] { '\u70B9', '\u83DC' }); // 点菜
           byte[] stringBytes = Encoding.Unicode.GetBytes(inputString);
          char[] stringChars = Encoding.Unicode.GetChars(stringBytes);
          StringBuilder builder = new StringBuilder();
          Array.ForEach<char>(stringChars, c => builder.AppendFormat("\\u{0:X}", (int) c));
          Console.WriteLine(builder);
    
          Console.ReadKey(true);
        }
      }
    }
    

     

    Marcel

    • Marked as answer by Noam B Tuesday, June 15, 2010 1:12 PM
    Tuesday, June 15, 2010 12:47 PM
  • Well, later I found a very easy way to do that, so for those of you that will search for a solution,

    here is a way to imitate the JavaScript escape command:

    Add a reference to Microsoft.JScript.dll

     

    string result = Microsoft.JScript.GlobalObject.escape(inputString);

     

    Noam B
    _________________________________________________________

    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Marked as answer by Noam B Thursday, June 17, 2010 12:31 PM
    Thursday, June 17, 2010 12:30 PM