4

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);
Nigje
  • 301
  • 1
  • 2
  • 9
  • @dasblinkenlight, I test `string result = Encoding.GetEncoding("all type").GetString(inpud)` or` Encoding.UTF8.GetBytes(input) and Encoding.UTF8.GetString` and many other trend. – Nigje May 24 '17 at 10:30
  • 1
    It's duplicate of https://stackoverflow.com/a/31960245/298573 – VahidN May 24 '17 at 15:19
  • @VahidN , i searched it but i don't see this question. i read your link an understand what is problem. by another search i fixed that. – Nigje May 24 '17 at 16:19

3 Answers3

1

You can use Encoding.Convert.

string source = // Your source
byte[] utfb = Encoding.UTF8.GetBytes(source);
byte[] resb = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("ISO-8859-6"), utfb);
string result = Encoding.GetEncoding("ISO-8859-6").GetString(resb);

NOTE: I wasn't sure which standard you wanted so for the example I used ISO-8859-6 (Arabic).

stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
1

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.

Nigje
  • 301
  • 1
  • 2
  • 9
  • Actually you are using `Encoding.GetEncoding("Windows-1252")` and `Encoding.Default` can be different in different settings and languages. – VahidN May 24 '17 at 20:10
1

I get bytes by UTF8 and Get String by Default as follow. This worked for me.

byte[] bytes = Encoding.UTF8.GetBytes(inputString);
resultString = Encoding.Default.GetString(bytes);
Kaveh Safavi
  • 499
  • 6
  • 5