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
+8.8k Yum Error: no such table: packages
+10.4k Golang : Generate random integer or float number
+5.2k Python : Create Whois client or function example
+18.2k Golang : Put UTF8 text on OpenCV video capture image frame
+4.7k Chrome : How to block socketloop.com links in Google SERP?
+23.1k Golang : simulate tail -f or read last line from log file example
+21.2k Golang : Clean up null characters from input data
+14.1k Golang : Check if a file exist or not
+29.4k Golang : JQuery AJAX post data to server and send data back to client example
+33.7k Golang : All update packages with go get command
+8.8k Golang : Heap sort example
+19.3k Golang : Get RGBA values of each image pixel