1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 00:22:41 +08:00

uppercase hex

This commit is contained in:
2022-05-27 10:58:22 +08:00
parent 3cc5b24d65
commit ebf862e40b
2 changed files with 29 additions and 2 deletions

View File

@ -3,9 +3,10 @@ package bytesconv
import ( import (
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"strings"
) )
const hextable = "0123456789ABCDEF"
type BytesResult []byte type BytesResult []byte
func (r BytesResult) Hex() string { func (r BytesResult) Hex() string {
@ -13,7 +14,15 @@ func (r BytesResult) Hex() string {
} }
func (r BytesResult) UppercaseHex() 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 { func (r BytesResult) Base64() string {

View File

@ -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)
}