Convert latin1 to UTF8 in C#

12,709

convert latin1 (ISO-8859-1) to UTF8 in C#:

Encoding.UTF8.GetString(Encoding.GetEncoding("iso-8859-1").getBytes(s))

OR

In C-Sharp use System.Text:

byte[] utf8Bytes = Encoding.UTF8.GetBytes("ASCII to UTF8");
byte[] isoBytes = Encoding.Convert(Encoding.ASCII, Encoding.UTF8, utf8Bytes);
string uf8converted = Encoding.UTF8.GetString(isoBytes);

Source:

Convert Latin 1 encoded UTF8 to Unicode

C# Convert string from UTF-8 to ISO-8859-1 (Latin1) H

Share:
12,709
hainv
Author by

hainv

Updated on June 04, 2022

Comments

  • hainv
    hainv almost 2 years

    Possible Duplicate:
    Convert Latin 1 encoded UTF8 to Unicode

    I want to convert latin1 (ISO-8859-1) to UTF8 in C#. What is the best way to do this?

    My string is "Công ty TNHH TM và DL Việt Hương".

  • hainv
    hainv over 11 years
    My string is "Công ty TNHH TM và DL Việt Hương" when using unicodetools.com/unicode/utf8-to-latin-converter.php result is correct but using Encoding.UTF8.GetString(Encoding.GetEncoding("iso-8859-1").g‌​etBytes(s)) is incorrect.
  • Eric Leschinski
    Eric Leschinski over 11 years
    The website converter for latin1 to utf8 must use an algorithm that can handle non latin1 characters when converting from latin1 to UTF8. So it appears C# is less tolerant when fed non-latin characters, wheras the website is able to make an educated guess about the invalid characters. The question becomes, which algorithm is the website using and in which language is it written in?
  • Michael Petrotta
    Michael Petrotta over 11 years
    Yeah, that site doesn't do a great job with the OP's string either. Not that I'd expect it to - without knowing the source codepage, it comes down to guesswork. I certainly wouldn't call it "correct".
  • Esailija
    Esailija over 11 years
    The website doesn't convert anything, your string was ok to begin with, with just html needing to be unescaped. Because the site is in html, they show correctly without any conversion even.