internal/hex_encode.go   A
last analyzed

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
dl 0
loc 23
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A internal.stringToHex 0 5 1
A internal.HexEncode 0 5 2
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