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 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us


Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

I'm working on some more Cheat Sheets book on Jquery, TypeScript, React js and for other languages. I will send it once it's completed.

Stay tuned working on React Js Cheat Sheets Book

Related Posts