Golang encoding/hex.Encode() function example

package encoding/hex

Encode encodes src(2nd parameter) into EncodedLen(len(src)) bytes of dst (1st parameter). As a convenience, it returns the number of bytes written to dst, but this value is always EncodedLen(len(src)). Encode implements hexadecimal encoding.

Golang encoding/hex.Encode() function usage example

 package main

 import (
 "encoding/hex"
 "fmt"
 )

 func main() {
 str := "abcdef"
 src := []byte(str)

 dst := make([]byte, hex.EncodedLen(len(src)))

 num := hex.Encode(dst, src)

 fmt.Printf("Number of bytes written : %v \n", num)
 }

Output :

Number of bytes written : 12

Reference :

http://golang.org/pkg/encoding/hex/#Encode

Advertisement