Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert from int to hex

Tags:

casting

go

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?

like image 955
KiYugadgeter Avatar asked Nov 07 '15 09:11

KiYugadgeter


1 Answers

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' 
like image 85
VonC Avatar answered Oct 14 '22 10:10

VonC