Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a string to UTF8?

I have a string that contains some unicode, how do I convert it to UTF-8 encoding?

like image 656
user705414 Avatar asked Jan 03 '12 04:01

user705414


People also ask

Is UTF-8 a string?

UTF-8 encodes a character into a binary string of one, two, three, or four bytes. UTF-16 encodes a Unicode character into a string of either two or four bytes. This distinction is evident from their names. In UTF-8, the smallest binary representation of a character is one byte, or eight bits.


4 Answers

This should be with the minimum code:

byte[] bytes = Encoding.Default.GetBytes(myString);
myString = Encoding.UTF8.GetString(bytes);
like image 158
Habeeb Avatar answered Oct 21 '22 05:10

Habeeb


Try this function, this should fix it out-of-box. You may need to fix naming conventions though.

private string UnicodeToUTF8(string strFrom)
{
    byte[] bytSrc;
    byte[] bytDestination;
    string strTo = String.Empty;

    bytSrc = Encoding.Unicode.GetBytes(strFrom);
    bytDestination = Encoding.Convert(Encoding.Unicode, Encoding.ASCII, bytSrc);
    strTo = Encoding.ASCII.GetString(bytDestination);

    return strTo;
}
like image 41
Arvin Avatar answered Oct 21 '22 06:10

Arvin


This snippet makes an array of bytes with your string encoded in UTF-8:

UTF8Encoding utf8 = new UTF8Encoding();
string unicodeString = "Quick brown fox";
byte[] encodedBytes = utf8.GetBytes(unicodeString);
like image 27
Sergey Kalinichenko Avatar answered Oct 21 '22 06:10

Sergey Kalinichenko


try to this code

 string unicodeString = "Quick brown fox";
 var bytes = new List<byte>(unicodeString);
        foreach (var c in unicodeString)
            bytes.Add((byte)c);
        var retValue = Encoding.UTF8.GetString(bytes.ToArray());
like image 41
Shyam sundar shah Avatar answered Oct 21 '22 06:10

Shyam sundar shah