3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Go】文字コード変換(UTF-8)

Posted at

準備

必要なパッケージをダウンロード

$ go get -u github.com/saintfish/chardet
$ go get -u golang.org/x/net/html/charset

サンプルコード

package main

import (
	"bytes"
	"io/ioutil"

	"github.com/saintfish/chardet"
	"golang.org/x/net/html/charset"
)

func toUtf8(b []byte) []byte {
	// 文字コード判定
	det := chardet.NewTextDetector()
	detRslt, _ := det.DetectBest(b)

	// 文字コード変換
	bReader := bytes.NewReader(b)
	reader, _ := charset.NewReaderLabel(detRslt.Charset, bReader)

	u8, _ := ioutil.ReadAll(reader)

	return u8
}

参考

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?