golang实现base64解码并转换为unicode转换为utf-8
时间: 2023-05-29 18:05:54 浏览: 333
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字符串: 你好世界
相关问题
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实现base转换为utf-8
package main
import (
"fmt"
"strings"
)
var base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
func baseToBinary(b byte) string {
idx := strings.IndexByte(base, b)
if idx == -1 {
return ""
}
return fmt.Sprintf("%06b", idx)
}
func binaryToUTF8(b string) string {
utf8 := ""
for len(b) > 0 {
r, size := utf8Decode(b)
utf8 += string(r)
b = b[size:]
}
return utf8
}
func utf8Decode(b string) (r rune, size int) {
if len(b) == 0 {
return 0, 0
}
firstByte := b[0]
if firstByte < 0x80 {
return rune(firstByte), 1
}
masks := []byte{0x7f, 0x3f, 0x1f, 0x0f}
mask := masks[(firstByte>>3)&0x03]
r = rune(firstByte & mask)
size = 2
if firstByte&0xe0 == 0xc0 {
mask = masks[0]
r |= rune((b[1] & mask) << 6)
} else if firstByte&0xf0 == 0xe0 {
mask = masks[1]
r |= rune((b[1] & mask) << 6)
r |= rune(b[2] & 0x3f)
size = 3
} else if firstByte&0xf8 == 0xf0 {
mask = masks[2]
r |= rune((b[1] & mask) << 6)
r |= rune((b[2] & 0x3f) << 6)
r |= rune(b[3] & 0x3f)
size = 4
}
return r, size
}
func baseToUTF8(s string) (string, error) {
if len(s)%4 != 0 {
return "", fmt.Errorf("invalid base string")
}
var binary string
for i := 0; i < len(s); i += 4 {
block := s[i : i+4]
binary += baseToBinary(block[0])
binary += baseToBinary(block[1])
binary += baseToBinary(block[2])
binary += baseToBinary(block[3])
}
return binaryToUTF8(binary), nil
}
func main() {
baseString := "SGVsbG8gV29ybGQh"
utf8String, err := baseToUTF8(baseString)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(utf8String) // Output: "Hello World!"
}