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 : Ackermann function example
+10.2k Golang : How to get quoted string into another string?
+14.3k Golang : Get uploaded file name or access uploaded files
+10.1k Golang : Identifying Golang HTTP client request
+6.2k Golang : Get missing location after unmarshal binary and gob decode time.
+11.6k Swift : Convert (cast) Float to String
+39.2k Golang : How to read CSV file
+8.6k Golang : Progress bar with ∎ character
+33.8k Golang : convert(cast) bytes to string
+29.9k Golang : How to get HTTP request header information?
+14k Golang : Reverse IP address for reverse DNS lookup example
+9.5k Golang : Changing a RGBA image number of channels with OpenCV