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
+10.6k Golang : Create S3 bucket with official aws-sdk-go package
+7.6k Golang : Tell color name with OpenCV example
+29.7k Get client IP Address in Go
+15.5k Golang : Get sub string example
+7k Golang : Rename part of filename
+10.4k Golang : Replace a parameter's value inside a configuration file example
+5.7k Golang : Extract XML attribute data with attr field tag example
+8.6k Golang : Capture text return from exec function example
+15.5k Golang : How to check if input from os.Args is integer?
+17.9k Golang : Send email with attachment
+13.4k Golang : Fix image: unknown format error
+8.5k Golang : Get curl -I or head data from URL example