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

I have googled on this topic and I looked at every answer, but I still don't understand.

Basically I need to convert the UTF-8 string to ISO-8859-1, and I do this using the following code:

Encoding iso = Encoding.GetEncoding("ISO-8859-1"); Encoding utf8 = Encoding.UTF8; string msg = iso.GetString(utf8.GetBytes(Message)); 

My source line

 Message = "ÄäÖöÕõÜü" 

But unfortunately my result string is getting

 msg = "�ä�ö�õ�ü 

What am I doing wrong here?

+90
c # encoding utf-8 iso-8859-1
Dec 17 '09 at 2:37 p.m.
source share
8 answers

Use Encoding.Convert to configure the byte array before trying to decode it into the target encoding.

 Encoding iso = Encoding.GetEncoding("ISO-8859-1"); Encoding utf8 = Encoding.UTF8; byte[] utfBytes = utf8.GetBytes(Message); byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes); string msg = iso.GetString(isoBytes); 
+138
Dec 17 '09 at 14:47
source share

I think your problem is that you assume that the bytes representing the utf8 string will lead to the same string when interpreted as something else (iso-8859-1). And this is simply not so. I recommend you read this excellent article by Joel spolsky.

+20
Dec 17 '09 at 14:45
source share

Try the following:

 Encoding iso = Encoding.GetEncoding("ISO-8859-1"); Encoding utf8 = Encoding.UTF8; byte[] utfBytes = utf8.GetBytes(Message); byte[] isoBytes = Encoding.Convert(utf8,iso,utfBytes); string msg = iso.GetString(isoBytes); 
+14
Dec 17 '09 at 14:47
source share

You need to fix the source code first.

A string in .NET is actually just an array of 16-bit Unicode code points, characters, so the string does not have any particular encoding.

This is when you take this string and convert it to a set of bytes that encode.

In any case, as you did, a string is encoded into an array of bytes with one character set, and then decodes it with another, will not work, as you can see.

Can you tell us more about where this original line came from, and why do you think it was encoded incorrectly?

+7
Dec 17 '09 at 14:44
source share

Seems a bit weird code. To get a string from the Utf8 byte stream, you just need to:

 string str = Encoding.UTF8.GetString(utf8ByteArray); 

If you need to save the iso-8859-1 byte stream, then just use: an extra line of code for the previous one:

 byte[] iso88591data = Encoding.GetEncoding("ISO-8859-1").GetBytes(str); 
+4
Jun 13 '14 at 8:54
source share

Just used the Nathan solution and it works great. I needed to convert ISO-8859-1 to Unicode:

 string isocontent = Encoding.GetEncoding("ISO-8859-1").GetString(fileContent, 0, fileContent.Length); byte[] isobytes = Encoding.GetEncoding("ISO-8859-1").GetBytes(isocontent); byte[] ubytes = Encoding.Convert(Encoding.GetEncoding("ISO-8859-1"), Encoding.Unicode, isobytes); return Encoding.Unicode.GetString(ubytes, 0, ubytes.Length); 
0
Jun 27. '14 at 13:55
source share
 Encoding targetEncoding = Encoding.GetEncoding(1252); // Encode a string into an array of bytes. Byte[] encodedBytes = targetEncoding.GetBytes(utfString); // Show the encoded byte values. Console.WriteLine("Encoded bytes: " + BitConverter.ToString(encodedBytes)); // Decode the byte array back to a string. String decodedString = Encoding.Default.GetString(encodedBytes); 
0
Oct 26 '14 at 1:55
source share

Here is an example for ISO-8859-9;

 protected void btnKaydet_Click(object sender, EventArgs e) { Response.Clear(); Response.Buffer = true; Response.ContentType = "application/vnd.openxmlformatsofficedocument.wordprocessingml.documet"; Response.AddHeader("Content-Disposition", "attachment; filename=XXXX.doc"); Response.ContentEncoding = Encoding.GetEncoding("ISO-8859-9"); Response.Charset = "ISO-8859-9"; EnableViewState = false; StringWriter writer = new StringWriter(); HtmlTextWriter html = new HtmlTextWriter(writer); form1.RenderControl(html); byte[] bytesInStream = Encoding.GetEncoding("iso-8859-9").GetBytes(writer.ToString()); MemoryStream memoryStream = new MemoryStream(bytesInStream); string msgBody = ""; string Email = "mail@xxxxxx.org"; SmtpClient client = new SmtpClient("mail.xxxxx.org"); MailMessage message = new MailMessage(Email, "mail@someone.com", "ONLINE APP FORM WITH WORD DOC", msgBody); Attachment att = new Attachment(memoryStream, "XXXX.doc", "application/vnd.openxmlformatsofficedocument.wordprocessingml.documet"); message.Attachments.Add(att); message.BodyEncoding = System.Text.Encoding.UTF8; message.IsBodyHtml = true; client.Send(message);} 
-5
Sep 17 '15 at 8:02
source share



All Articles