Total Lines | 23 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |