forked from go/golangs_learn
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.8 KiB
85 lines
1.8 KiB
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"github.com/axgle/mahonia"
|
|
_ "golang.org/x/text/encoding/simplifiedchinese"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Charset string
|
|
|
|
const (
|
|
UTF8 = Charset("UTF-8")
|
|
GB18030 = Charset("GB18030")
|
|
)
|
|
|
|
/* 实现文本编码格式转换 */
|
|
func main() {
|
|
//创建日志文件
|
|
t := time.Now()
|
|
filepath := "./log_" + strings.Replace(t.String()[:19], ":", "_", 3) + ".txt"
|
|
file, err := os.OpenFile(filepath, os.O_CREATE, 0666)
|
|
if err != nil {
|
|
log.Fatal("create log file failed!")
|
|
}
|
|
defer file.Close()
|
|
wFile := bufio.NewWriter(file)
|
|
wFile.WriteString(readfile())
|
|
wFile.Flush()
|
|
}
|
|
|
|
/* 读取文本并转码 */
|
|
func readfile() string {
|
|
f, err := os.Open("ex7.txt")
|
|
if err != nil {
|
|
return err.Error()
|
|
}
|
|
defer f.Close()
|
|
buf := make([]byte, 1024)
|
|
//文件ex7.txt的编码是gb18030
|
|
decoder := mahonia.NewDecoder("gb18030")
|
|
if decoder == nil {
|
|
return "编码不存在!"
|
|
}
|
|
var str string = ""
|
|
for {
|
|
n, _ := f.Read(buf)
|
|
if 0 == n {
|
|
break
|
|
}
|
|
//解码为UTF-8
|
|
str += decoder.ConvertString(string(buf[:n]))
|
|
}
|
|
return str
|
|
}
|
|
|
|
/* 字符串从srcCode格式转为tagCode的格式 */
|
|
/*func ConvertToString(src string, srcCode string, tagCode string) string {
|
|
srcCoder := mahonia.NewDecoder(srcCode)
|
|
srcResult := srcCoder.ConvertString(src)
|
|
tagCoder := mahonia.NewDecoder(tagCode)
|
|
_, cdata, _ := tagCoder.Translate([]byte(srcResult), true)
|
|
result := string(cdata)
|
|
return result
|
|
|
|
}*/
|
|
|
|
/* golang处理中文时默认是utf8,当遇到其他如GBK字符是就会出现乱码 编码转换 */
|
|
/*
|
|
func ConvertByte2String(byte []byte, charset Charset) string {
|
|
var str string
|
|
switch charset {
|
|
case GB18030:
|
|
var decodeBytes, _ = simplifiedchinese.GB18030.NewDecoder().Bytes(byte)
|
|
str = string(decodeBytes)
|
|
case UTF8:
|
|
fallthrough
|
|
default:
|
|
str = string(byte)
|
|
}
|
|
return str
|
|
}*/
|
|
|