Help us understand the problem. What is going on with this article?

[Go]文字コードのUTF-8変換を行う - charset decode

実現したい機能

Shift-JIS EUC-JP ISO-2022-JP等の文字コードであるstringをGo内部で扱えるよう UTF-8 にデコードする。

実装

文字コードが既に分かっている場合の変換は以下の手順で実現できる。

  • 入力stringのcharsetに応じたdecoderを取得
  • decoderを用いてstringをUTF-8にdecode
package main

import (
    "errors"
    "fmt"
    "golang.org/x/net/html/charset"
    "golang.org/x/text/transform"
)

// Converts the specified charset to UTF-8.
func Decode(src []byte, charSet string) (string, error) {
    e, _ := charset.Lookup(charSet)
    if e == nil {
        return string(src), errors.New(fmt.Sprintf("invalid charset [%s]", charSet))
    }
    decodeStr, _, err := transform.Bytes(
        e.NewDecoder(),
        src,
    )
    if err != nil {
        return string(src), err
    }
    return string(decodeStr), nil
}

使い方

func TestDecode(t *testing.T) {
    t.Run("SJIS -> UTF-8", func(t *testing.T) {
        src := <SJIS byte slice>
        // encodingの指定は"sjis"や"Shift_JIS"でも可
        decoded, err := Decode(src, "shift_jis")
        fmt.Println(decoded)
    })
}

その他

今回のソースコードは以下に置いておきます。
https://github.com/tomtwinkle/go-encoding-decoder

tomtwinkle
golangおいしいです。
beex-inc
BeeXではクラウドネイティブアプリ開発、企業の基幹クラウド基盤構築、システム移行、運用保守を行っています。
https://www.beex-inc.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした