c# How to encode arabic characters to utf8?
11,457
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.
Author by
Paradigm
Updated on June 05, 2022Comments
-
Paradigm almost 2 years
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
-
Paradigm over 8 yearsresult is : clientName is same before encoding
-
rufanov over 8 years@Paradigm, yes, as it should. Or you expected something else?
-
Paradigm over 8 yearsi expected characters like Óèïåçäèðÿ
-
rufanov over 8 years@Paradigm, ehm... for what purpose? Just for fun? Or to prank some client with "ah, it's weird characters on screen! your data is totally broken! pay us for repair!"?
-
rufanov over 8 years@Paradigm, i updated answer with solution to your task.