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
Author by
hainv
Updated on June 04, 2022Comments
-
hainv almost 2 years
Possible Duplicate:
Convert Latin 1 encoded UTF8 to UnicodeI 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 over 11 yearsMy 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").getBytes(s)) is incorrect.
-
Eric Leschinski over 11 yearsThe 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 over 11 yearsYeah, 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 over 11 yearsThe 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.