11

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

Jim D'Angelo
  • 3,924
  • 3
  • 23
  • 39
user705414
  • 19,158
  • 37
  • 109
  • 154
  • 1
    i think this helps to you. http://stackoverflow.com/questions/497782/how-to-convert-a-string-from-utf8-to-ascii-single-byte-in-c – Bishan Jan 03 '12 at 04:04

4 Answers4

22

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);
Sergey Kalinichenko
  • 697,062
  • 78
  • 1,055
  • 1,465
  • 8
    Why not just use `Encoding.UTF8.GetBytes` rather than `new`ing up another encoder? – Jesse C. Slicer Jan 03 '12 at 04:10
  • 1
    @JesseC.Slicer This snippet is from one of [Microsoft's examples for the UTF8Encoding class](http://msdn.microsoft.com/en-us/library/system.text.utf8encoding.aspx). I am not 100% certain why they choose to do it this way, but I would assume it's for thread safety (they mention that instance members are not guaranteed to be thread safe, but this is only my guess). – Sergey Kalinichenko Jan 03 '12 at 04:17
  • @CAS I have no idea, please ask a separate question. – Sergey Kalinichenko Aug 27 '19 at 14:07
4

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;
}
CarenRose
  • 1,234
  • 1
  • 13
  • 21
Arvin
  • 1,152
  • 8
  • 8
  • Use this to make your strings 1. XML compatible before saving to database, 2. (or) CSV compatible before exporting to CSV – Arvin Oct 16 '15 at 13:53
3

This should be with the minimum code:

byte[] bytes = Encoding.Default.GetBytes(myString);
myString = Encoding.UTF8.GetString(bytes);
Habeeb
  • 6,865
  • 1
  • 27
  • 31
1

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());
Shyam sundar shah
  • 2,341
  • 1
  • 23
  • 37