Simple_Code
Simple_Code

Reputation: 40

How to convert Arabic string to UTF-8 using C#?

I need to convert some arabic text to utf-8 and convert it to Hexa I made some codes but it turns the output to like what in next image.

Codes I trid :

string myName = _name.Text;
string myNameLength  = _name.TextLength.ToString("X2");
byte[] nameByte = Encoding.Default.GetBytes(myName);
var hexStringName = BitConverter.ToString(nameByte);
hexStringCo = hexStringCo.Replace("-", "");

Picture enter image description here

Upvotes: 1

Views: 272

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062510

Getting the utf8 bytes is:

string name = "عبود";
byte[] utf8 = Encoding.UTF8.GetBytes(name);
var hex = BitConverter.ToString(utf8);
hex = hex.Replace("-", "");
Console.WriteLine(hex); // D8B9D8A8D988D8AF

What you do with those is up to you; there's zero chance that a hex string was rendered with the replacement character (aka �), so: you're doing something else that we can't see. Maybe show us what you're doing with the value once you have it.

Upvotes: 2

Related Questions