Skip to content

Instantly share code, notes, and snippets.

@hyamamoto
Created February 7, 2015 17:05
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyamamoto/db03c03fd624881d4b84 to your computer and use it in GitHub Desktop.
Save hyamamoto/db03c03fd624881d4b84 to your computer and use it in GitHub Desktop.
ShiftJIS <=> UTF-8 conversion samples in Go language.
package main
import (
"fmt"
"strings"
"bytes"
"io"
"io/ioutil"
"golang.org/x/text/transform"
// "code.google.com/p/go.text/transform" // deprecated
"golang.org/x/text/encoding/japanese"
// "code.google.com/p/go.text/encoding/japanese" // deprecated
)
func transformEncoding( rawReader io.Reader, trans transform.Transformer) (string, error) {
ret, err := ioutil.ReadAll(transform.NewReader(rawReader, trans))
if err == nil {
return string(ret), nil
} else {
return "", err
}
}
// Convert a string encoding from ShiftJIS to UTF-8
func FromShiftJIS(str string) (string, error) {
return transformEncoding(strings.NewReader(str), japanese.ShiftJIS.NewDecoder())
}
// Convert a string encoding from UTF-8 to ShiftJIS
func ToShiftJIS(str string) (string, error) {
return transformEncoding(strings.NewReader(str), japanese.ShiftJIS.NewEncoder())
}
// Convert an array of bytes (a valid ShiftJIS string) to a UTF-8 string
func BytesFromShiftJIS(b []byte) (string, error) {
return transformEncoding(bytes.NewReader(b), japanese.ShiftJIS.NewDecoder())
}
// Convert an array of bytes (a valid UTF-8 string) to a ShiftJIS string
func BytesToShiftJIS(b []byte) (string, error) {
return transformEncoding(bytes.NewReader(b), japanese.ShiftJIS.NewEncoder())
}
func main() {
strRaw := "\x8c\x8e\x93\xfa\x82\xcd\x95\x53\x91\xe3\x82\xcc\x89\xdf\x8b\x71" +
"\x82\xc9\x82\xb5\x82\xc4\x81\x41\x8d\x73\x82\xa9\x82\xd3\x94\x4e" +
"\x82\xe0\x96\x94\x97\xb7\x90\x6c\x96\xe7\x81\x42"
strUTF := "月日は百代の過客にして、行かふ年も又旅人也。"
// string (ShiftJIS) -> []byte (ShiftJIS)
bytesRaw := []byte(strRaw)
// string (UTF-8) -> []byte (UTF-8)
bytesUTF := []byte(strUTF)
// string (ShiftJIS) -> string (UTF-8)
{
result, err := FromShiftJIS(strRaw)
if err != nil {
panic(err)
}
fmt.Println(result)
}
// []byte (ShiftJIS) -> string (UTF-8)
{
result, err := BytesFromShiftJIS(bytesRaw)
if err != nil {
panic(err)
}
fmt.Println(result)
}
// []byte (UTF-8) -> string (ShiftJIS)
{
result, err := BytesToShiftJIS(bytesUTF)
if err != nil {
panic(err)
}
fmt.Printf("ByteToShiftJIS: %t", result == strRaw)
}
}
@BinodKafle
Copy link

ioutil.ReadAll is DEPRECATED. Now we can simply use io.ReadAll

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment