如何将 []bytes 转换为十六进制?

huangapple go评论77阅读模式
英文:

How to go from []bytes to get hexadecimal

问题

func md(str string) string {
    h := md5.New()
    io.WriteString(h, str)

    return fmt.Sprintf("%x", h.Sum(nil))
}

// Example usage:
hashKey := md("example@example.com")
fmt.Println(hashKey)

以上是将输入字符串转换为哈希键字符串的代码示例。通过使用fmt.Sprintf("%x", h.Sum(nil)),可以将哈希键以%x格式返回。你可以使用这个函数将电子邮件地址转换为哈希键,并用它来访问Gravatar.com。

希望对你有帮助!

英文:

http://play.golang.org/p/SKtaPFtnKO

func md(str string) []byte {
    h := md5.New()
    io.WriteString(h, str)

    fmt.Printf("%x", h.Sum(nil))
    // base 16, with lower-case letters for a-f
    return h.Sum(nil)
}

All I need is Hash-key string that is converted from an input string. I was able to get it in bytes format usting h.Sum(nil) and able to print out the Hash-key in %x format. But I want to return the %x format from this function so that I can use it to convert email address to Hash-key and use it to access Gravatar.com.

How do I get %x format Hash-key using md5 function in Go?

Thanks,

答案1

得分: 70

如果我理解正确,你想要返回%x格式:

你可以导入"encoding/hex"并使用"EncodeToString"方法:

str := hex.EncodeToString(h.Sum(nil))

或者只需使用Sprintf函数:

func md(str string) string {
    h := md5.New()
    io.WriteString(h, str)
    
    return fmt.Sprintf("%x", h.Sum(nil))
}

请注意,Sprintf函数较慢,因为它需要解析格式字符串,然后根据找到的类型进行反射。

http://play.golang.org/p/vsFariAvKo

英文:

If I understood correctly you want to return the %x format:

you can import "encoding/hex" and use the EncodeToString method

str := hex.EncodeToString(h.Sum(nil))

or just Sprintf the value:

func md(str string) string {
	h := md5.New()
	io.WriteString(h, str)
	
	return fmt.Sprintf("%x", h.Sum(nil))
}

note that Sprintf is slower because it needs to parse the format string and then reflect based on the type found

http://play.golang.org/p/vsFariAvKo

答案2

得分: 23

你应该避免使用fmt包来进行这个操作。fmt包使用了反射,除了调试之外,对于其他任何操作都是昂贵的。你知道你拥有什么,以及想要转换成什么,所以你应该使用适当的转换包。

要将二进制转换为十六进制,可以使用encoding/hex包。

转换为十六进制字符串:

str := hex.EncodeToString(h.Sum(nil))

从十六进制字符串转换:

b, err := hex.DecodeString(str)

该包还提供了对[]byte进行编码/解码的函数。

当你需要转换为/从十进制时,可以使用strconv包。

int转换为string

str := strconv.Itoa(100)

string转换为int

num, err := strconv.Atoi(str)

该包中还有其他一些函数可以进行其他类型的转换(进制等)。

所以除非你在调试或格式化错误消息,请使用适当的转换。谢谢。

英文:

You should avoid using the fmt package for this. The fmt package uses reflection, and it is expensive for anything other than debugging. You know what you have, and what you want to convert to, so you should be using the proper conversion package.

For converting from binary to hex, and back, use the encoding/hex package.

To Hex string:

str := hex.EncodeToString(h.Sum(nil))

From Hex string:

b, err := hex.DecodeString(str)

There are also Encode / Decode functions for []byte.

When you need to convert to / from a decimal use the strconv package.

From int to string:

str := strconv.Itoa(100)

From string to int:

num, err := strconv.Atoi(str)

There are several other functions in this package that do other conversions (base, etc.).

So unless you're debugging or formatting an error message, use the proper conversions. Please.

huangapple
  • 本文由 发表于 2013年10月12日 07:56:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/19328957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定