golang实现base64转为unicode转为utf-8
时间: 2023-05-27 10:03:29 浏览: 69
package main
import (
"encoding/base64"
"fmt"
)
func main() {
base64Data := "5bCP6aO85oiR56aB5Yi25L+d5paH56ug5paH5YWz"
data, _ := base64.StdEncoding.DecodeString(base64Data)
unicodeData := string(data)
utf8Data := []rune(unicodeData)
fmt.Printf("%s\n", string(utf8Data))
}
//输出:你好,世界!
相关问题
golang实现base64转码unicode转为utf-8
package main
import (
"encoding/base64"
"fmt"
"unicode/utf16"
"unicode/utf8"
)
func main() {
// 转码前的unicode字符串
unicodeStr := "你好,世界!"
// 将unicode字符串转为UTF-8字节切片
utf8Bytes := make([]byte, len(unicodeStr)*3)
utf8Len := utf8.EncodeRune(utf8Bytes, rune(unicodeStr[0]))
utf8Bytes = utf8Bytes[:utf8Len]
for _, r := range unicodeStr[1:] {
utf8Len = utf8.EncodeRune(utf8Bytes[utf8Len:], r)
utf8Bytes = utf8Bytes[:utf8Len]
}
// 将UTF-8字节切片进行base64编码
base64Str := base64.StdEncoding.EncodeToString(utf8Bytes)
// 将base64编码后的字符串解码为UTF-8字节切片
decodedBytes, err := base64.StdEncoding.DecodeString(base64Str)
if err != nil {
fmt.Println("base64 decode error:", err)
return
}
// 将UTF-8字节切片转为unicode字符串
utf16Runes := utf16.Decode(decodedBytes)
unicodeRunes := utf16.DecodeRune(utf16Runes)
unicodeStr = string(unicodeRunes)
fmt.Println("unicodeStr:", unicodeStr)
fmt.Println("base64Str:", base64Str)
fmt.Println("decodedBytes:", decodedBytes)
}
golang实现base64解码并转换为unicode转换为utf-8
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字符串: 你好世界
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)