How to print hexadecimal in golang?

Hi Friends 👋,

Welcome To aGuideHub! ❤️

Today, I’m going to show you how to convert decimal to hexadecimal and how we can show it in golang console.

Here, we will use the fmt.Sprintf() method to convert decimal to hexadecimal and fmt.Printf() method for show hexadecimal value in golang console.

Let’s write code.

Following code, we are storeing i := 255 255 value in variable, converting decimal to hexadecimal and storeing in h variable h := fmt.Sprintf("%x", i).

After that, you can see how we are printing variable values in the console using the fmt.Printf() method.

package main
import "fmt"

func main() {
  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)

When we run above code, we will see decimal and hexadeciaml both data in console, check following output image.

print, hexadecimal, golang

Try it yourself

All the best 👍

Follow me on Twitter