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
+25.2k Golang : Storing cookies in http.CookieJar example
+16.4k CodeIgniter/PHP : Create directory if does not exist example
+10.1k Golang : Edge detection with Sobel method
+26.9k Golang : Force your program to run with root permissions
+6.5k Grep : How to grep for strings inside binary data
+22.6k Generate checksum for a file in Go
+15.2k Golang : How to add color to string?
+7.7k Gogland : Where to put source code files in package directory for rookie
+7.1k Golang : Get environment variable
+25.8k Golang : Daemonizing a simple web server process example
+10k Golang : Read file and convert content to string
+6.3k Apt-get to install and uninstall Golang