golang 十六进制编码为大写字符串?

use*_*b89 1 encoding hex go

go编码的最佳方法是什么:hex.EncodeToString

https://golang.org/pkg/encoding/hex/#EncodeToString

但变成大写字母?

icz*_*cza 5

您可以致电查询strings.ToUpper()结果:

src := []byte("Hello")

s := hex.EncodeToString(src)
fmt.Println(s)

s = strings.ToUpper(s)
fmt.Println(s)
Run Code Online (Sandbox Code Playgroud)

或者你可以fmt.Sprintf()%X动词一起使用:

s = fmt.Sprintf("%X", src)
fmt.Println(s)
Run Code Online (Sandbox Code Playgroud)

上面的输出(在Go Playground上尝试一下):

48656c6c6f
48656C6C6F
48656C6C6F
Run Code Online (Sandbox Code Playgroud)

如果性能很重要,请实现您自己的编码器。看一下源码encoding/hex。这真的很简单:

const hextable = "0123456789abcdef"

func EncodeToString(src []byte) string {
    dst := make([]byte, EncodedLen(len(src)))
    Encode(dst, src)
    return string(dst)
}

func Encode(dst, src []byte) int {
    j := 0
    for _, v := range src {
        dst[j] = hextable[v>>4]
        dst[j+1] = hextable[v&0x0f]
        j += 2
    }
    return len(src) * 2
}
Run Code Online (Sandbox Code Playgroud)

是的,您所需要做的就是更改hextable为包含大写字母。