golang实现base64转码unicode转为utf-8
时间: 2023-05-29 11:05:28 浏览: 214
可对utf-8内容进行base64编码后内容解码
package main
import (
"encoding/base64"
"fmt"
"strconv"
"strings"
)
func main() {
// base64转码
str := "Hello, 世界"
encoded := base64.StdEncoding.EncodeToString([]byte(str))
fmt.Println(encoded)
// unicode转utf-8
str = "\\u4e2d\\u56fd\\u4eba"
unicodeArr := strings.Split(str, "\\u")[1:]
var utf8Arr []byte
for _, unicode := range unicodeArr {
code, _ := strconv.ParseInt(unicode, 16, 32)
utf8Arr = append(utf8Arr, byte(code))
}
utf8Str := string(utf8Arr)
fmt.Println(utf8Str)
}
// 输出:
// SGVsbG8sIOS4lueVjA==
// 中国人
阅读全文