Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to "iso-8859-1"?

Tags:

c#

asp.net

How can i convert an UTF-8 string into an ISO-8859-1 string?

like image 969
Tom Avatar asked Mar 10 '09 10:03

Tom


2 Answers

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);
like image 188
John Feminella Avatar answered Sep 28 '22 04:09

John Feminella


.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");
like image 38
mmx Avatar answered Sep 28 '22 06:09

mmx