Encode/Decode base64

前端 未结 5 1016
陌清茗
陌清茗 2021-02-06 22:06

here is my code and i don\'t understand why the decode function doesn\'t work.

Little insight would be great please.

func EncodeB64(message string) (reto         


        
5条回答
  •  执念已碎
    2021-02-06 22:41

    The len prefix is superficial and causes the invalid utf-8 error:

    package main
    
    import (
            "encoding/base64"
            "fmt"
            "log"
    )
    
    func main() {
            str := base64.StdEncoding.EncodeToString([]byte("Hello, playground"))
            fmt.Println(str)
    
            data, err := base64.StdEncoding.DecodeString(str)
            if err != nil {
                    log.Fatal("error:", err)
            }
    
            fmt.Printf("%q\n", data)
    }
    

    (Also here)


    Output

    SGVsbG8sIHBsYXlncm91bmQ=
    "Hello, playground"
    

    EDIT: I read too fast, the len was not used as a prefix. dystroy got it right.

提交回复
热议问题