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:
@ -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 {
|
||||||
|
18
bytesconv/bytes_result_test.go
Normal file
18
bytesconv/bytes_result_test.go
Normal 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)
|
||||||
|
}
|
Reference in New Issue
Block a user