go utf-8转其他编码

栏目: Go · 发布时间: 5年前

package main

import (
	"golang.org/x/text/transform"
	"bytes"
	"golang.org/x/text/encoding/charmap"
	"golang.org/x/text/encoding/simplifiedchinese"
	"io/ioutil"
	"fmt"
	"math/rand"
	"time"
)

func main() {
	rand.Seed(time.Now().Unix())
	fmt.Println(string(convert(rand.Intn(47),[]byte("golang学习"))))
}

func convert(i int, s []byte) ([]byte) {
	var reader *transform.Reader
	switch i {
	case 1:
		reader = transform.NewReader(bytes.NewReader(s), charmap.ISO8859_1.NewDecoder())
	case 2:
		reader = transform.NewReader(bytes.NewReader(s), charmap.ISO8859_2.NewDecoder())
	case 3:
		reader = transform.NewReader(bytes.NewReader(s), charmap.ISO8859_3.NewDecoder())
	case 4:
		reader = transform.NewReader(bytes.NewReader(s), charmap.ISO8859_4.NewDecoder())
	case 5:
		reader = transform.NewReader(bytes.NewReader(s), charmap.ISO8859_5.NewDecoder())
	case 6:
		reader = transform.NewReader(bytes.NewReader(s), charmap.ISO8859_6.NewDecoder())
	case 7:
		reader = transform.NewReader(bytes.NewReader(s), charmap.ISO8859_7.NewDecoder())
	case 8:
		reader = transform.NewReader(bytes.NewReader(s), charmap.ISO8859_8.NewDecoder())
	case 9:
		reader = transform.NewReader(bytes.NewReader(s), charmap.ISO8859_9.NewDecoder())
	case 10:
		reader = transform.NewReader(bytes.NewReader(s), charmap.ISO8859_10.NewDecoder())
	case 11:
		reader = transform.NewReader(bytes.NewReader(s), charmap.ISO8859_13.NewDecoder())
	case 12:
		reader = transform.NewReader(bytes.NewReader(s), charmap.ISO8859_14.NewDecoder())
	case 13:
		reader = transform.NewReader(bytes.NewReader(s), charmap.ISO8859_15.NewDecoder())
	case 14:
		reader = transform.NewReader(bytes.NewReader(s), charmap.ISO8859_16.NewDecoder())
	case 15:
		reader = transform.NewReader(bytes.NewReader(s), charmap.KOI8R.NewDecoder())
	case 16:
		reader = transform.NewReader(bytes.NewReader(s), charmap.KOI8U.NewDecoder())
	case 17:
		reader = transform.NewReader(bytes.NewReader(s), charmap.Macintosh.NewDecoder())
	case 18:
		reader = transform.NewReader(bytes.NewReader(s), charmap.MacintoshCyrillic.NewDecoder())
	case 19:
		reader = transform.NewReader(bytes.NewReader(s), charmap.Windows874.NewDecoder())
	case 20:
		reader = transform.NewReader(bytes.NewReader(s), charmap.Windows1250.NewDecoder())
	case 21:
		reader = transform.NewReader(bytes.NewReader(s), charmap.Windows1251.NewDecoder())
	case 22:
		reader = transform.NewReader(bytes.NewReader(s), charmap.Windows1252.NewDecoder())
	case 23:
		reader = transform.NewReader(bytes.NewReader(s), charmap.Windows1253.NewDecoder())
	case 24:
		reader = transform.NewReader(bytes.NewReader(s), charmap.Windows1254.NewDecoder())
	case 25:
		reader = transform.NewReader(bytes.NewReader(s), charmap.Windows1255.NewDecoder())
	case 26:
		reader = transform.NewReader(bytes.NewReader(s), charmap.Windows1256.NewDecoder())
	case 27:
		reader = transform.NewReader(bytes.NewReader(s), charmap.Windows1257.NewDecoder())
	case 28:
		reader = transform.NewReader(bytes.NewReader(s), charmap.Windows1258.NewDecoder())
	case 29:
		reader = transform.NewReader(bytes.NewReader(s), charmap.XUserDefined.NewDecoder())
	case 30:
		reader = transform.NewReader(bytes.NewReader(s), charmap.CodePage037.NewDecoder())
	case 31:
		reader = transform.NewReader(bytes.NewReader(s), charmap.CodePage437.NewDecoder())
	case 32:
		reader = transform.NewReader(bytes.NewReader(s), charmap.CodePage850.NewDecoder())
	case 33:
		reader = transform.NewReader(bytes.NewReader(s), charmap.CodePage852.NewDecoder())
	case 34:
		reader = transform.NewReader(bytes.NewReader(s), charmap.CodePage855.NewDecoder())
	case 35:
		reader = transform.NewReader(bytes.NewReader(s), charmap.CodePage858.NewDecoder())
	case 36:
		reader = transform.NewReader(bytes.NewReader(s), charmap.CodePage860.NewDecoder())
	case 37:
		reader = transform.NewReader(bytes.NewReader(s), charmap.CodePage862.NewDecoder())
	case 38:
		reader = transform.NewReader(bytes.NewReader(s), charmap.CodePage863.NewDecoder())
	case 39:
		reader = transform.NewReader(bytes.NewReader(s), charmap.CodePage865.NewDecoder())
	case 40:
		reader = transform.NewReader(bytes.NewReader(s), charmap.CodePage865.NewDecoder())
	case 41:
		reader = transform.NewReader(bytes.NewReader(s), charmap.CodePage866.NewDecoder())
	case 42:
		reader = transform.NewReader(bytes.NewReader(s), charmap.CodePage1047.NewDecoder())
	case 43:
		reader = transform.NewReader(bytes.NewReader(s), charmap.CodePage1140.NewDecoder())
	case 44:
		reader = transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewEncoder())
	case 45:
		reader = transform.NewReader(bytes.NewReader(s), simplifiedchinese.HZGB2312.NewEncoder())
	case 46:
		reader = transform.NewReader(bytes.NewReader(s), simplifiedchinese.GB18030.NewEncoder())
	}

	d, _ := ioutil.ReadAll(reader)

	return d
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

用户体验面面观

用户体验面面观

[美] 库涅夫斯基(Mike Kuniavsky) / 汤海 / 清华大学出版社 / 2010-5 / 69.00

这是一本专注于用户研究和用户体验的经典读物,同时也是一本容易上手的实战手册,从实践者的角度,着重讨论和阐述了用户研究的重要性、主要的用户研究方法和工具,同时借助于实例介绍了相关的应用。全书共3部分18章,深度剖析了何为优秀的用户设计,用户体验包括哪些研究方法和工具,如何 得出和分析用户体验调查结果等。一起来看看 《用户体验面面观》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具