Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert utf-8 string to Persian unicode

Tags:

c#

arabic

persian

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);
like image 880
Nigje Avatar asked Sep 12 '25 18:09

Nigje


1 Answers

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).

like image 54
stelioslogothetis Avatar answered Sep 14 '25 10:09

stelioslogothetis