Search code examples
c#arabicpersian

convert utf-8 string to Persian unicode


I have following string as utf-8. i want convert it to persian unicode:

ابراز داشت: امام رضا برخال� دیگر ائمه با جنگ نرم

this site correctly do convert and result is: ابراز داشت: امام رضا برخالف دیگر ائمه با جنگ نرم

I test many method and ways but can't resolve this problem, for example these two lines did not produce the desired result:

string result = Encoding.GetEncoding("all type").GetString(input);

and

byte[] preambleBytes= Encoding.UTF8.GetPreamble();
byte[] inputBytes= Encoding.UTF8.GetBytes(input);
byte[] resultBytes= preambleBytes.Concat(inputBytes).ToArray();

string result=Encoding.UTF8.GetString(resultBytes.ToArray());
string resultAscii=Encoding.Ascii.GetString(inputBytes);
string resultUnicode=Encoding.Unicode.GetString(inputBytes);

Solution

  • I understand what is problem by reading What is problem and Solution .

    when i converted string to byte[], i forced that to convert as utf-8 format but really i should use default format for converting.

    False converting: 
    byte[] bytes = Encoding.UTF8.GetBytes(inputString);
    resultString = Encoding.UTF8.GetString(bytes);
    

    But

    True converting:
    byte[] bytes = Encoding.Default.GetBytes(inputString);
    resultString = Encoding.UTF8.GetString(bytes);
    

    Tanks for your comments and answers.