Golang encoding/hex.Decode() function example
package encoding/hex
Decode decodes src(2nd parameter) into DecodedLen(len(src)) bytes, returning the actual number of bytes written to dst (1st parameter). If Decode encounters invalid input, it returns an error describing the failure.
Golang encoding/hex.Decode() function usage example :
package main
import (
"encoding/hex"
"fmt"
"os"
)
func main() {
str := "abcd"
src := []byte(str)
dst := make([]byte, hex.DecodedLen(len(src)))
num, err := hex.Decode(dst, src) // <----- here
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Number of bytes written : %v", num)
}
Output :
Number of bytes written : 2
If Decode encounter error, it will explain why. For example :
encoding/hex: odd length hex string
exit status 1
Reference :
Advertisement
Something interesting
Tutorials
+6.8k Golang : Get expvar(export variables) to work with multiplexer
+11.8k Golang : convert(cast) float to string
+11.2k Golang : Calculate Relative Strength Index(RSI) example
+15k Golang : How do I get the local IP (non-loopback) address ?
+9k Golang : Build and compile multiple source files
+5.4k Golang : What is StructTag and how to get StructTag's value?
+21.1k Golang : Sort and reverse sort a slice of strings
+6.3k Javascript : Generate random key with specific length
+9.5k Golang : Changing a RGBA image number of channels with OpenCV
+5.4k Golang : Get S3 or CloudFront object or file information
+19.4k Golang : Fix cannot download, $GOPATH not set error
+9.6k Golang : Copy map(hash table) example