Golang encoding/base32.NewEncoder function examples

package encoding/base32

NewEncoder returns a new base32 stream encoder. Data written to the returned writer will be encoded using encoder type defined in 1st parameter and written to 2nd parameter. Base32 encodings operate in 5-byte blocks; when finished writing, the caller must Close the returned encoder to flush any partially written blocks.

Golang encoding/base32.NewEncoder function examples

Example 1 :

 // Base32ExtEncode encodes binary data to base32 extended (RFC 4648) encoded text.
 func Base32ExtEncode(data []byte) (text []byte) {
 n := base32.HexEncoding.EncodedLen(len(data))
 buf := bytes.NewBuffer(make([]byte, 0, n))
 encoder := base32.NewEncoder(base32.HexEncoding, buf)
 encoder.Write(data)
 encoder.Close()
 if buf.Len() != n {
 panic("internal error")
 }
 return buf.Bytes()
 }

Example 2 :

 // Encodes things read from stdin into base32.

 package main
 import (
 "encoding/base32"
 "fmt"
 "io"
 "os"
 )

 func main() {
 enc := base32.NewEncoder(base32.StdEncoding, os.Stdout)
 if _, err := io.Copy(enc, os.Stdin); err != nil {
 fmt.Fprintf(os.Stderr, "%s\n", err)
 os.Exit(1)
 }
 enc.Close()
 fmt.Println()
 }

References :

http://golang.org/pkg/encoding/base32/#NewEncoder

https://github.com/cznic/strutil/blob/master/strutil.go

https://github.com/dchest/base32util/blob/master/main.go

Advertisement