https://github.com/timakin/gonvert
Golang character encoding converter with an automatic code-estimation.
https://github.com/timakin/gonvert
character character-encoding character-encoding-converter converter encoder encoding golang
Last synced: 2 months ago
JSON representation
Golang character encoding converter with an automatic code-estimation.
- Host: GitHub
- URL: https://github.com/timakin/gonvert
- Owner: timakin
- Created: 2016-12-31T11:04:52.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-01-12T00:02:42.000Z (over 8 years ago)
- Last Synced: 2025-03-18T07:12:09.132Z (2 months ago)
- Topics: character, character-encoding, character-encoding-converter, converter, encoder, encoding, golang
- Language: Go
- Homepage:
- Size: 46.9 KB
- Stars: 26
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Gonvert
====Simple character-encoding converter with an automatic character-code detection in Golang.
You can convert without a declaration of a previous encoding.[](https://travis-ci.org/timakin/gonvert)
[](https://coveralls.io/github/timakin/gonvert)## Install
```
go get github.com/timakin/gonvert
```## Current Support
- Shift_JIS <-> UTF8
- Shift_JIS <-> EUC-JP
- Shift_JIS <-> GBK
- EUC-JP <-> UTF8
- EUC-JP <-> GBK
- GBK <-> UTF8
- UTF8 <-> UTF16You can specify the character code to encode/decode with gonvert constatants.
Prepared `const` character-code is following.
```
const (
UTF8 CharCode = iota
SJIS
EUCJP
GBK
UTF16BE
UTF16LE
)
```## Usage
You can call the converter with 2 or 3 arguements.
If you set 2 variables, gonvert will estimate the code automatically.
But if you already know the code of strings, you should set the third arguements, without an estimation.
```
package mainimport (
"github.com/timakin/gonvert"
"fmt"
)
func main() {
// ------------ Estimation case ------------// Input a Shift_JIS encoded string
sjisStr := "\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"
converter := gonvert.New(sjisStr, gonvert.UTF8)
result, err := converter.Convert()
if err != nil {
panic("Failed to convert!")
}
// This will print out the utf-8 encoded string: "月日は百代の過客にして、行かふ年も又旅人也。"
fmt.Print(result)// -----------------------------------------
// ------------ Specified-code case ------------
sjisStr := "\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"// Should send `before` character code if you already know, because an estimation like above may become incorrect.
converter := gonvert.New(sjisStr, gonvert.UTF8, gonvert.SJIS)
result, err := converter.Convert()
if err != nil {
panic("Failed to convert!")
}
fmt.Print(result)// -----------------------------------------
}
```