diff --git a/bytesconv/byteResult.go b/bytesconv/byteResult.go index cb0cba4..fc72915 100644 --- a/bytesconv/byteResult.go +++ b/bytesconv/byteResult.go @@ -3,9 +3,10 @@ package bytesconv import ( "encoding/base64" "encoding/hex" - "strings" ) +const hextable = "0123456789ABCDEF" + type BytesResult []byte func (r BytesResult) Hex() string { @@ -13,7 +14,15 @@ func (r BytesResult) Hex() string { } func (r BytesResult) UppercaseHex() string { - return strings.ToUpper(hex.EncodeToString(r)) + dst := make([]byte, hex.EncodedLen(len(r))) + j := 0 + for _, v := range r { + dst[j] = hextable[v>>4] + dst[j+1] = hextable[v&0x0f] + j += 2 + } + + return BytesToString(dst) } func (r BytesResult) Base64() string { diff --git a/bytesconv/bytes_result_test.go b/bytesconv/bytes_result_test.go new file mode 100644 index 0000000..4f3dbcf --- /dev/null +++ b/bytesconv/bytes_result_test.go @@ -0,0 +1,18 @@ +package bytesconv_test + +import ( + "testing" + + "github.com/charlienet/go-mixed/bytesconv" + "github.com/charlienet/go-mixed/rand" +) + +func TestHexUppercase(t *testing.T) { + b, _ := rand.RandBytes(12) + + l := bytesconv.BytesResult(b).Hex() + t.Log(l) + + u := bytesconv.BytesResult(b).UppercaseHex() + t.Log(u) +}