dongyuying1507 2013-10-11 23:56
浏览 91
已采纳

如何从[]个字节开始获取十六进制

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,

  • 写回答

2条回答 默认 最新

  • douhanxujiuji6098 2013-10-12 00:09
    关注

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

    you can import 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)