To convert an integer to its hexadecimal representation as a string in Golang, you can use the fmt.Sprintf
function with the format specifier %x
. Here's an example:
package main
import (
"fmt"
)
func main() {
num := 123456789
hex := fmt.Sprintf("%x", num)
fmt.Println(hex)
}
In this example, fmt.Sprintf
is used to convert the integer num
to its hexadecimal string representation and store it in the variable hex
. The %x
format specifier is used to specify that the conversion should be done in hexadecimal format.
When you run this code, it will output:
75bcd15
which is the hexadecimal representation of the integer 123456789
.