编码/解码base64 [英] Encode/Decode base64
本文介绍了编码/解码base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码,我不明白为什么解码功能不起作用.
here is my code and i don't understand why the decode function doesn't work.
请很少有见识的人.
func EncodeB64(message string) (retour string) {
base64Text := make([]byte, base64.StdEncoding.EncodedLen(len(message)))
base64.StdEncoding.Encode(base64Text, []byte(message))
return string(base64Text)
}
func DecodeB64(message string) (retour string) {
base64Text := make([]byte, base64.StdEncoding.DecodedLen(len(message)))
base64.StdEncoding.Decode(base64Text, []byte(message))
fmt.Printf("base64: %s\n", base64Text)
return string(base64Text)
}
它给了我: [解码错误-输出不是utf-8] [解码错误-输出不是utf-8]
It gaves me : [Decode error - output not utf-8][Decode error - output not utf-8]
推荐答案
DecodedLen
返回最大长度.
此长度对于调整缓冲区大小很有用,但是部分缓冲区不会被写入,因此将不是有效的UTF-8.
This length is useful for sizing your buffer but part of the buffer won't be written and thus won't be valid UTF-8.
您只需要使用Decode
函数返回的实际书写长度.
You have to use only the real written length returned by the Decode
function.
l, _ := base64.StdEncoding.Decode(base64Text, []byte(message))
log.Printf("base64: %s\n", base64Text[:l])
这篇关于编码/解码base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文