| Total Lines | 25 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 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 | } |
||
| 28 |