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'\n", i, h)
h = fmt.Sprintf("%X", i)
fmt.Printf("HEX conv of '%d' is '%s'\n", i, h)
输出:
Hex conv of '255' is 'ff'
HEX conv of '255' is 'FF'