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 playgroundi := 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'
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报