'Unicode to Arabic Win1256

I have Unicodes in my Database which were encoded from Arabic and I need some source code to convert it to actual Arabic. I have tried a lot of codes but not working for me and I am tired now. Can someone please help me to resolve.

This picture shows the right result with Unicodes used:

image

For example

"ÇÈÑÇåíã ãåäÇ ãÍãÏ ÇáãåäÇ" 

should return

"ابراهيم مهنا محمد المهنا". 


Solution 1:[1]

It seems, that you mix encodings:

string original = @"??????? ???? ???? ??????";

byte[] bytes = Encoding.GetEncoding(1256).GetBytes(original);

and you get correct Win-1256 byte array:

{c7 c8 d1 c7 e5 ed e3 20 e3 e5 e4 c7 20 e3 cd e3 cf 20 c7 e1 e3 e5 e4 c7}

then erroneously treat the array as unicode:

{00 c7 00 c8 00 d1 00 c7, ..., 00 c7}   

however, 00 c7 is not an arabic symbol any more. It's latin. To restore original string you can try

string source = "ÇÈÑÇåíã ãåäÇ ãÍãÏ ÇáãåäÇ";

string result = Encoding
  .GetEncoding(1256)              
  .GetString(Encoding               
     .Unicode                      // read as unicode 
     .GetBytes(source)             
     .Where((b, i) => i % 2 == 0)  // odd bytes only
     .ToArray());

Console.WriteLine(result);

Outcome:

??????? ???? ???? ??????

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitry Bychenko