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
}

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

查看所有标签

猜你喜欢:

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

Java性能优化权威指南

Java性能优化权威指南

Charlie Hunt、Binu John / 柳飞、陆明刚 / 人民邮电出版社 / 2014-3 / 109.00 元

Java性能优化圣经!Java之父重磅推荐! 本书由曾任职于Oracle/Sun的性能优化专家编写,系统而详细地讲解了性能优化的各个方面,帮助你学习Java虚拟机的基本原理、掌握一些监控Java程序性能的工具,从而快速找到程序中的性能瓶颈,并有效改善程序的运行性能。 Java性能优化的任何问题,都可以从本书中找到答案!一起来看看 《Java性能优化权威指南》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换