golang实现base64解码并转换为unicode转换为utf-8
时间: 2023-05-29 22:05:54 浏览: 503
package main
import (
"encoding/base64"
"fmt"
"strconv"
"strings"
)
func main() {
str := "5Yiw5paH5a2X56ym5Lq6"
// Base64解码
bytes, err := base64.StdEncoding.DecodeString(str)
if err != nil {
fmt.Println("解码失败:", err)
return
}
// 转换为Unicode码点
var unicodePoints []string
for _, b := range bytes {
unicodePoints = append(unicodePoints, fmt.Sprintf("%04x", b))
}
unicodeStr := strings.Join(unicodePoints, "")
// 转换为UTF-8
var utf8Bytes []byte
for i := 0; i < len(unicodeStr); i += 4 {
u, _ := strconv.ParseInt(unicodeStr[i:i+4], 16, 32)
utf8Bytes = append(utf8Bytes, byte(u))
}
fmt.Println("解码后的UTF-8字符串:", string(utf8Bytes))
}
// 输出:解码后的UTF-8字符串: 你好世界
阅读全文