Home > Enterprise >  Convert utf8 string to ISO-8859-1
Convert utf8 string to ISO-8859-1

Time:10-23

How to convert a utf8 string to ISO-8859-1 in golang

Have tried to search but can only find conversions the other way and the few solutions I found didn't work

I need to convert string with special danish chars like æ, ø and å

ø => ø etc.

CodePudding user response:

Keep in mind that ISO-8859-1 only supports a tiny subset of characters compared to Unicode. If you know for certain that your UTF-8 encoded string only contains characters covered by ISO-8859-1, you can use the following code.

package main

import (
    "fmt"

    "golang.org/x/text/encoding/charmap"
)

func main() {
    str := "Räv"

    encoder := charmap.ISO8859_1.NewEncoder()
    out, err := encoder.Bytes([]byte(str))
    if err != nil {
        panic(err)
    }

    fmt.Printf("%x\n", out)
}

The above prints:

52e476

So 0x52, 0xE4, 0x76, which looks correct as per https://en.wikipedia.org/wiki/ISO/IEC_8859-1 - in particular the second character is of note, since it would be encoded as 0xC3, 0xA4 in UTF-8.

If the string contains characters that aren't supported, e.g. we change str to be "Räv

  • Related