mushroomsir / iconv

πŸš€ Pure golang implement

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

About

πŸš€ Pure golang implement


Languages

Language:Go 100.0%