[英]Golang decode base64 data to string
I am trying to decode a simple base64 encoded string in golang. 我试图在golang中解码一个简单的base64编码字符串。
package main
import b64 "encoding/base64"
import fmt
func main() {
data := "YmFzZTY0IGVuY29kZWQgc3RyaW5n"
sDec, err1 := b64.StdEncoding.DecodeString(data)
fmt.Println(sDec)
fmt.Println(err1)
}
And the output I get is 我得到的输出是
[98 97 115 101 54 52 32 101 110 99 111 100 101 100 32 115 116 114 105 110 103] // this is the decoded data.
<nil>
Where as https://www.base64decode.net/ decodes and outputs as 其中https://www.base64decode.net/解码并输出为
base64 encoded string
How do I get the same result in golang? 如何在golang中获得相同的结果?
StdEncoding.DecodeString(data)
(which is base64.Encoding.DecodeString()
) returns you the decoded data as byte slice ( []byte
), and this is what you see printed: the decimal representation of the individal bytes. StdEncoding.DecodeString(data)
(它是base64.Encoding.DecodeString()
)将解码后的数据作为字节切片( []byte
)返回,这就是你看到的打印:个别字节的十进制表示。
Convert the byte slice to string
if you want to see it as a string
value: 转换的字节切片string
,如果你想看到它作为一个string
值:
fmt.Println(string(sDec))
Or use a format string where you specify you want to print is as a string
: 或者使用您指定要打印的格式字符串作为string
:
fmt.Printf("%s\n", sDec)
Then the output will be (try it on the Go Playground ): 然后输出将是(在Go Playground上尝试):
base64 encoded string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.