How can i convert an UTF-8 string into an ISO-8859-1 string?
Asked
Active
Viewed 4.2k times
13
-
5There are only UTF-16 strings in .NET. *Anything* else is an array of bytes. – Richard Mar 10 '09 at 11:37
2 Answers
23
Try:
System.Text.Encoding iso_8859_1 = System.Text.Encoding.GetEncoding("iso-8859-1");
System.Text.Encoding utf_8 = System.Text.Encoding.UTF8;
// Unicode string.
string s_unicode = "abcéabc";
// Convert to ISO-8859-1 bytes.
byte[] isoBytes = iso_8859_1.GetBytes(s_unicode);
// Convert to UTF-8.
byte[] utf8Bytes = System.Text.Encoding.Convert(iso_8859_1, utf_8, isoBytes);

John Feminella
- 303,634
- 46
- 339
- 357
-
-
Thanks, but how can I output this: Response.Write(isoBytes) does not work? – Tom Mar 10 '09 at 10:55
-
byte[] srcBytes = utf_8.GetBytes(srcstr); byte[] tgtBytes = System.Text.Encoding.Convert(utf_8, iso_8859_1, srcBytes); Response.Write(tgtBytes); – Tom Mar 10 '09 at 10:57
5
.NET Strings are all UTF-16 internally. There is no UTF-8 or ISO-8859-1 encoded System.String
in .NET. To get the binary representation of the string in a particular encoding use System.Text.Encoding
class:
byte[] bytes = Encoding.GetEncoding("iso-8859-1").GetBytes("hello world");

Mehrdad Afshari
- 414,610
- 91
- 852
- 789
-
-
@leppie: What's a UTF8 or ISO-88590-1 string? Are those anything except a sequence of bytes? – Mehrdad Afshari Mar 10 '09 at 10:42