internal.HexEncode   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
//HexEncode returns empty string if the parameter is unsupported type.
9
//Just works with all primitive types.
10
//In decimal numbers, it should be used as a "string" in numbers where the total number of digits is too many.
11
func HexEncode(a interface{}) (string, error) {
12
	if s, err := ToString(a); err == nil {
13
		return stringToHex(s)
14
	} else {
15
		return "", err
16
	}
17
}
18
19
func stringToHex(s string) (string, error) {
20
	src := []byte(s)
21
	dst := make([]byte, hex.EncodedLen(len(src)))
22
	hex.Encode(dst, src)
23
	return fmt.Sprintf("%s", dst), nil
24
}
25