Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# How to encode arabic characters to utf8?

i try to encode Arabic characters to utf8 as sample

string clientName="على";
Encoding iso = Encoding.GetEncoding(1256);
Encoding utf8 = Encoding.UTF8;
byte[] utfBytes = utf8.GetBytes(clientName);
byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
string clientNameArabic = iso.GetString(isoBytes);

but i think it is not correct so, i need help

like image 536
Paradigm Avatar asked Nov 27 '25 21:11

Paradigm


1 Answers

You don't need to convert your string to Windows-1256 charset. It's just:

// on server side(before sending as byte array)
var bytes = Encoding.UTF8.GetBytes(clientName);

// on client side(after reciving byte array)
clientName = Encoding.UTF8.GetString(bytes);

Also in most situation's(except when using very low-level IO, like network sockets, or binary file streams) you don't need to think about encoding conversion at all, because UTF-8 is default encoding that used in .NET high-level I/O operations.


To accomplish your weird task of converting normal Unicode string على to broken mojibake with symbols like Óèïåçäèðÿ you should write your string using Arabic Windows Encoding(1256), then read it back using Western European Windows Encoding(1252) like this:

var source = "على";

var westernLatin = Encoding.GetEncoding(1252);
var arabic = Encoding.GetEncoding(1256);

var bytes = arabic.GetBytes(source);
var result = westernLatin.GetString(bytes); // Uáì

No need to use UTF-8 here.

like image 131
rufanov Avatar answered Nov 29 '25 10:11

rufanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!