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?
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)
Output:
Hex conv of '255' is 'ff' HEX conv of '255' is 'FF'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With