Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert ansi text to utf8

Tags:

utf-8

go

ansi

How to convert ansi text to utf8 in Go? I am trying to convert ansi string to utf8 string.

like image 934
pynet Avatar asked Aug 03 '11 13:08

pynet


2 Answers

Go only has UTF-8 strings. You can convert something to a UTF8 string using the conversion described here from a byte[]:

http://golang.org/doc/go_spec.html#Conversions

like image 199
Deleted Avatar answered Oct 09 '22 11:10

Deleted


Here is newer method.

package main    
import (
    "bytes"
    "fmt"
    "io/ioutil"    
    "golang.org/x/text/encoding/traditionalchinese"
    "golang.org/x/text/transform"
)    
func Decode(s []byte) ([]byte, error) {
    I := bytes.NewReader(s)
    O := transform.NewReader(I, traditionalchinese.Big5.NewDecoder())
    d, e := ioutil.ReadAll(O)
    if e != nil {
        return nil, e
    }
    return d, nil
}
func main() {
    s := []byte{0xB0, 0xAA}
    b, err := Decode(s)
    fmt.Println(string(b))
    fmt.Println(err)
}

I were use iconv-go to do such convert, you must know what's your ANSI code page, in my case, it is 'big5'.

package main
import (
    "fmt"
    //iconv "github.com/djimenez/iconv-go"
    iconv "github.com/andelf/iconv-go"
    "log"
)

func main() {
    ibuf := []byte{170,76,80,67}
    var obuf [256]byte

    // Method 1: use Convert directly
    nR, nW, err := iconv.Convert(ibuf, obuf[:], "big5", "utf-8")
    if err != nil {
        log.Fatalln(err)
    }
    log.Println(nR, ibuf)
    log.Println(obuf[:nW])
    fmt.Println(string(obuf[:nW]))

    // Method 2: build a converter at first
    cv, err := iconv.NewConverter("big5", "utf-8")
    if err != nil {
        log.Fatalln(err)
    }
    nR, nW, err = cv.Convert(ibuf, obuf[:])
    if err != nil {
        log.Fatalln(err)
    }
    log.Println(string(obuf[:nW]))
}
like image 29
Daniel YC Lin Avatar answered Oct 09 '22 11:10

Daniel YC Lin