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.4k Golang : Generate Datamatrix barcode
+7k Golang : constant 20013 overflows byte error message
+11.6k Golang : Surveillance with web camera and OpenCV
+8.1k Golang : Randomize letters from a string example
+4.5k Java : Generate multiplication table example
+8.1k Golang : Get all countries phone codes
+41k Golang : How to check if a string contains another sub-string?
+17.2k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+6.4k Golang : Break string into a slice of characters example
+5.1k Golang : Display packages names during compilation
+9.5k Golang : Get all countries currencies code in JSON format
+6k Linux/MacOSX : Search for files by filename and extension with find command