Golang encoding/hex.DecodeString() function example

package encoding/hex

DecodeString returns the bytes represented by the hexadecimal string s (1st parameter).

Golang encoding/hex.DecodeString() function usage example

 package main

 import (
 "encoding/hex"
 "fmt"
 "os"
 )

 func main() {
 str := "abcd"

 b, err := hex.DecodeString(str)

 if err != nil {
 fmt.Println(err)
 os.Exit(1)
 }

 fmt.Printf("Decoded bytes %v \n ", b)
 }

Output :

Decoded bytes [171 205]

IF there is an error in decoding the string, it will look like this :

encoding/hex: invalid byte: U+00E4 'ä'

exit status 1

Reference :

http://golang.org/pkg/encoding/hex/#DecodeString

Advertisement