Skip to content

Latest commit

 

History

History
72 lines (60 loc) · 2.18 KB

README.md

File metadata and controls

72 lines (60 loc) · 2.18 KB

iconv

Build Status Coverage Status License GoDoc

Installation

go get -u github.com/mushroomsir/iconv

Support charset

  • UTF-8
  • GBK
  • GB-18030
  • GB2312
  • Big5
  • ISO-8859-1
  • EUC-JP
  • Shift_JIS
  • More coming soon

Usage

import (
	github.com/mushroomsir/iconv
)

Converting string Values

Converting a string can be done with two methods. First, there's iconv.ConvertString(input, fromEncoding, toEncoding string) syntactic sugar.

output,err := iconv.ConvertString("Hello World!", iconv.GBK, iconv.UTF8)

Alternatively, you can create a converter and use its ConvertString method. Reuse of a Converter instance is recommended when doing many string conversions between the same encodings.

converter := iconv.NewConverter(iconv.GBK, iconv.UTF8)
output,err := converter.ConvertString("Hello World!")

Converting []byte Values

Converting a []byte can similarly be done with two methods. First, there's iconv.Convert(input []byte, fromEncoding, toEncoding string).

input := []byte("Hello World!")
output, err := iconv.ConvertBytes(input, iconv.GBK, iconv.UTF8)

Just like with ConvertString, there is also a Convert method on Converter that can be used.

convert,err := iconv.NewConverter(iconv.GBK, iconv.UTF8)
input := []byte("Hello World!")
output, err := converter.ConvertBytes(input)

Converting an io.Reader

The iconv.Reader allows any other *io.Reader to be wrapped and have its bytes transcoded as they are read.

reader,err := iconv.Convert(strings.NewReader("Hello World!"),  iconv.GBK, iconv.UTF8)

Licenses

All source code is licensed under the MIT License.