internal.HexDecode   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
dl 0
loc 5
rs 10
c 0
b 0
f 0
nop 1
1
package internal
2
3
import (
4
	"encoding/hex"
5
	"fmt"
6
)
7
8
//HexDecode returns empty string with an error if the parameter is unsupported type.
9
//Just works with all primitive types
10
//If given parameter is different type instead of string, firstly, it will convert the given parameter to string with gotil.ToString().
11
func HexDecode(a interface{}) (string, error) {
12
	if s, err := ToString(a); err == nil {
13
		return hexToString(s)
14
	} else {
15
		return "", err
16
	}
17
}
18
19
func hexToString(s string) (string, error) {
20
	src := []byte(s)
21
	dst := make([]byte, hex.DecodedLen(len(src)))
22
	if n, err := hex.Decode(dst, src); err == nil {
23
		return fmt.Sprintf("%s", dst[:n]), nil
24
	} else {
25
		return "", err
26
	}
27
}
28