Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, June 15, 2010 11:39 AM
How can I convert a string to Unicode?
The output should be the Unicode string.
Noam B.
All replies (6)
Tuesday, June 15, 2010 12:47 PM ✅Answered | 1 vote
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
Thursday, June 17, 2010 12:30 PM ✅Answered | 1 vote
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...
Tuesday, June 15, 2010 12:03 PM
Could you give an example of what you want? C# strings are already Unicode.
Tuesday, June 15, 2010 12:16 PM | 1 vote
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
Tuesday, June 15, 2010 12:19 PM
Almost.
I need the \u at the beginning of each series also...
Tuesday, June 15, 2010 12:32 PM
uniString += string.Format("{0}{1}", @"\u", b.ToString("X"));
Manish Sati