11

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

Jim D'Angelo
  • 3,952
  • 3
  • 25
  • 39
user705414
  • 20,472
  • 39
  • 112
  • 155
  • 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
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 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,266
  • 1
  • 12
  • 24
Arvin
  • 1,232
  • 9
  • 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
  • 7,601
  • 1
  • 30
  • 33
2

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,473
  • 1
  • 25
  • 40