Converting Decimal to Hexa decimal Value

Hi Team,
I need to convert Decimal value to Hex Decimal value using GO.Is there any method available for converting this?

Thanks,
Rajapandian.B

I do assume, that you get a string as input representing the decimal number and want to return a string that represents the same number but in hexadecimal.

You can use the strconv package. Your input base is 10 and your output base is 16.

Of course you could also implement a direct conversion yourself. But as this is a very common task for homework, I will leave those implementations for you. If you need to implement yourself, you will need to show your implementation and we will help you to get it right.

1 Like

Using strconv

var n int64 = 1500
hex := strconv.FormatInt(n, 16)
fmt.Println(hex) 

Using fmt, in case you need a string:

var n int64 = 1500
hex := fmt.Sprintf("%x", n)
fmt.Println(hex)
2 Likes

Thanks matiasb.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.