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
+14.4k Golang : Recombine chunked files example
+8.4k Golang : Generate Datamatrix barcode
+10.2k Golang : Text file editor (accept input from screen and save to file)
+6.7k Golang : Humanize and Titleize functions
+9.7k Golang : Detect number of active displays and the display's resolution
+5.8k Cash Flow : 50 days to pay your credit card debt
+22.2k Golang : How to run Golang application such as web server in the background or as daemon?
+11.9k Golang : Determine if time variables have same calendar day
+10.9k Golang : Sieve of Eratosthenes algorithm
+14.5k Golang : Rename directory
+6.1k Golang : Grab news article text and use NLP to get each paragraph's sentences
+14.8k Golang : Get URI segments by number and assign as variable example