Base64 to UTF-8 String decoding- Arabic Text

12,062

Solution 1

After searching for few days. I came up with this and is working..

byte[] plain = Convert.FromBase64String(data);
Encoding iso = Encoding.GetEncoding("ISO-8859-6");
newData = iso.GetString(plain);
return newData;

Solution 2

You should run this under debugger and see whether you get the correct Arabic text in string text:

  • If text is incorrect, then The bytes (after Base64 decode) are not encoded as UTF-8, but some other encoding - UTF-16, Windows-1256, etc.
  • If text is correct, then it gets corrupted when displayed on the ASP.NET page. In that case, you should set the page's encoding to one that supports Arabic - best is UTF-8, as Shekhar suggests.
Share:
12,062
Chandra Eskay
Author by

Chandra Eskay

Learning everything from C to NodeJS

Updated on June 26, 2022

Comments

  • Chandra Eskay
    Chandra Eskay almost 2 years

    I'm trying to decode an Base64 data which contains a mixture of English and Arabic characters. I'm using the following code to decode.

    var bytes = Convert.FromBase64String(data); //data contains base64 data
    string text = Encoding.UTF8.GetString(bytes);
    

    After decoding I'm displaying it on the ASP page. My problem here is, English text is displayed properly whereas in place of arabic text i'm getting empty boxes and question marks like this. ����� ���

    Please suggest where i'm going wrong.

  • Esailija
    Esailija about 11 years
    This is Java and wouldn't even work there because it relies on platform default encoding.