Thx a lot !
and i found another way to do that in the mail list :)
Archos,
While the base64 package may be awkward and not very idiomatic for
your purpose, that doesn't mean it's wrong. You could simply provide
a
nicer view of the package yourself.
For example,
package main
import (
"fmt"
"encoding/base64"
)
func Encode(encBuf, bin []byte, e64 *base64.Encoding) []byte {
maxEncLen := e64.EncodedLen(len(bin))
if encBuf == nil || len(encBuf) < maxEncLen {
encBuf = make([]byte, maxEncLen)
}
e64.Encode(encBuf, bin)
return encBuf[0:]
}
func Decode(decBuf, enc []byte, e64 *base64.Encoding) []byte {
maxDecLen := e64.DecodedLen(len(enc))
if decBuf == nil || len(decBuf) < maxDecLen {
decBuf = make([]byte, maxDecLen)
}
n, err := e64.Decode(decBuf, enc)
_ = err
return decBuf[0:n]
}
func main() {
// binary data
bin := []byte("testing base64..")
// base64 standard encoding
e64 := base64.StdEncoding
// encode
enc := Encode(nil, bin, e64)
// decode
dec := Decode(nil, enc, e64)
// results
fmt.Println("Equal:", string(bin) == string(dec))
fmt.Println("bin:", len(bin), bin)
fmt.Println("enc:", len(enc), string(enc))
fmt.Println("dec:", len(dec), dec)
}
Which, using the idiomatic Go slice, gives the following neat and
tidy
and intuitive results.
bin: 16 [116 101 115 116 105 110 103 32 98 97 115 101 54 52 46 46]
enc: 24 dGVzdGluZyBiYXNlNjQuLg==
dec: 16 [116 101 115 116 105 110 103 32 98 97 115 101 54 52 46 46]
Peter
On Apr 30, 7:10 am, Archos <
raul....@sent.com> wrote: