Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
2.65/5 (6 votes)
See more:
I want to convert from Unicode to its original format UTF8 I think and it could be arabic like below. The user types some unicode text and when he clicks on convert it'll be converted to the original format like below :

C#
string unicode ="\u0633\u0637\u0648\u0631 \u0639\u0628\u0631 \u0627\u0644\u0623\u064a\u0627\u0645 1";


output must equal the original format:

سطور عبر الأيام 1 | شمس الدين خ

i used http://msdn.microsoft.com/en-us/library/vstudio/kdcak6ye%28v=vs.100%29.aspx[^]

as a reference to me but the output is the same like the input nothing happened no conversion has been happened could anyone advice me what's wrong?

What I have tried:

C#
private void btn_convert_Click(object sender, EventArgs e)
        {
            string unicodestring = txt_unicode.Text;
            
            // Create two different encodings.
            Encoding ascii = Encoding.ASCII;
            Encoding unicode = Encoding.Unicode;
            //Encoding Utf8 = Encoding.UTF8;

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

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

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

     // // Display the strings created before and after the conversion.
     //MessageBox.Show("Original string is"+unicodeString);
     MessageBox.Show("Ascii converted string is" + asciiString);
}
Posted
Updated 20-Jun-18 1:40am
v5
Comments
Richard MacCutchan 22-Jan-14 7:16am    
You cannot use GetBytes on a string in that form. You must first convert all the escape sequences to their numeric values to make a 'real' Unicode string.
PIEBALDconsult 23-Mar-18 23:12pm    
I don't see what the issue is. Why would any conversion be required? When I plug that statement into VS I get:
سطور عبر الأيام 1

You can use this function:
System.Uri.UnescapeDataString(string)


I test it for you:

C#
string unicode = "\u0633\u0637\u0648\u0631 \u0639\u0628\u0631 \u0627\u0644\u0623\u064a\u0627\u0645 1";
            string str = System.Uri.UnescapeDataString(unicode);


result (str content) is:
سطور عبر الأيام 1
 
Share this answer
 
Comments
Akhand Jyoti 3-Jan-18 6:10am    
hey i have tried this code work for that particular unicode srting.
I have a unicode string like.
string str="~\0\u0016?\0\u0013?\0AT??\\?\u0001#yfgyhvkh#?";
after doing the above operation i am not getting the converted string .please help me .Thank u so much in advance.
I should receive the data like:
7E 00 15 90 00 13 A2 00 41 54 EC 8E 5C 9F 01 23 31 32 23 31 32 23 52 23 0B
Simply use:

var output = System.Net.WebUtility.HtmlDecode(text);
 
Share this answer
 
Comments
Maciej Los 20-Jun-18 8:50am    
4 years too late!
Amir No-Family 12-Jul-18 6:29am    
better late than never!
Finally i get the solution if anyone need some reference

C#
private string convert_from_unicode(string str,char c)

{

    string rtstr = "";
    for (int i=2;i< str.Length; i+=6)
    {
            string str1 = str.Substring(i, 4);
            c = (char)Int16.Parse(str1, System.Globalization.NumberStyles.HexNumber);
            rtstr += c;
     }
    return rtstr;

}
private void btn_convert_Click(object sender, EventArgs e)
{

    char f = ' ';

    txt_result.Text = convert_from_unicode(txt_unicode.Text, f);

  }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900