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.2k Golang : Find relative luminance or color brightness
+9.2k Golang : Write multiple lines or divide string into multiple lines
+8.8k Golang : On lambda, anonymous, inline functions and function literals
+8k Golang : Handle Palindrome string with case sensitivity and unicode
+9.3k Golang : How to get ECDSA curve and parameters data?
+11.9k Golang : Convert(cast) bigint to string
+14k Golang : convert rune to unicode hexadecimal value and back to rune character
+8.2k Android Studio : Rating bar example
+15.9k Golang : Read a file line by line
+25.8k Golang : Daemonizing a simple web server process example
+7.9k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+6.3k Golang : Test input string for unicode example