dtt2012 2015-11-07 09:59
浏览 217
已采纳

我如何从int转换为hex

I want to convert from int to hex in Golang. In strconv, there is a method that converts strings to hex. Is there a similar method to get a hex string from an int?

  • 写回答

3条回答 默认 最新

  • duancao1951 2015-11-07 11:27
    关注

    Since hex is a Integer literal, you can ask the fmt package for a string representation of that integer, using fmt.Sprintf(), and the %x or %X format.
    See playground

    i := 255
    h := fmt.Sprintf("%x", i)
    fmt.Printf("Hex conv of '%d' is '%s'
    ", i, h)
    h = fmt.Sprintf("%X", i)
    fmt.Printf("HEX conv of '%d' is '%s'
    ", i, h)
    

    Output:

    Hex conv of '255' is 'ff'
    HEX conv of '255' is 'FF'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?