-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
用utf8转成gbk再转回去 转不回去了 #7
Comments
UTF-8 to GBK: 浣犲ソ |
既然是转换, 那你就应该使用Encoder, 把你的 ConverToString 修改一下就可以了: func ConvertToString(src string, srcCode string, tagCode string) string {
srcCoder := convlib.NewDecoder(srcCode)
srcResult := srcCoder.ConvertString(src)
//fmt.Println("srcResult["+srcCode+"]=", src, " => ", srcResult)
tagCoder := convlib.NewEncoder(tagCode)
result := tagCoder.ConvertString(srcResult)
//fmt.Println("toResult["+tagCode+"]=", srcResult, " => ", result)
return result
} 修改后的结果为: PS:以上是在UTF-8 中打印,只有UTF8编码是正常,其他编码是乱的. |
其实可以优化一下, 也就是 如果其中有一方是 UTF8 , 那么则不需要转换 ,反正 UTF8 转UTF8 是一样的。 如果srcCode desCode 都不是 UTF8 , 其实会使用UTF8 作为中间层,转换。 |
有现成的吗🙂 |
package main
import (
"fmt"
"github.com/axgle/mahonia"
)
func main() {
}
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
}
The text was updated successfully, but these errors were encountered: