library: encoding/base32

This commit is contained in:
xushiwei
2024-07-30 00:30:58 +08:00
parent afdf31a66c
commit ad1a42d6a5
2 changed files with 20 additions and 1 deletions

View File

@@ -1,11 +1,12 @@
package main
import (
"encoding/base32"
"encoding/base64"
"fmt"
)
func main() {
func base64Demo() {
msg := "Hello, 世界"
encoded := base64.StdEncoding.EncodeToString([]byte(msg))
fmt.Println(encoded)
@@ -16,3 +17,20 @@ func main() {
}
fmt.Println(string(decoded))
}
func base32Demo() {
str := "JBSWY3DPFQQHO33SNRSCC==="
dst := make([]byte, base32.StdEncoding.DecodedLen(len(str)))
n, err := base32.StdEncoding.Decode(dst, []byte(str))
if err != nil {
fmt.Println("decode error:", err)
return
}
dst = dst[:n]
fmt.Printf("%q\n", dst)
}
func main() {
base64Demo()
base32Demo()
}