Search code examples
utf-8goiso-8859-1

ISO-8859-1 to UTF8 conversion


The following snippet converts ISO-8859-1 encoded text to UTF8. I don't exactly understand what's going on here. Can someone explain why this works?

var utf8Buf bytes.Buffer
for _, b := range iso8859Slice {
  utf8Buf.WriteRune(rune(b))
}
utf8Str := utf8Buf.String()

Solution

  • The loop takes each byte of the iso8859Str slice assuming it is of type []byte

    Because iso-8859-1 is incorperated as the first 256 code points of Unicode, you have no need of actual conversion from iso-8859-1 to Unicode.

    However, what you need to do is to UTF-8 encode the Unicode rune. This is done by Buffer.WriteRune()

    WriteRune appends the UTF-8 encoding of Unicode code point r to the buffer