I'm having some issues on trying to convert an int
to hex then, padding it with 0
s in order to get a 6 Characters String
which represents the hex number.
So far, I tried the following:
intNumber := 12
hexNumber := strconv.FormatInt(intNumber, 16) //not working
And then I found out how to pad it with 0
s, using %06d, number/string
. It makes all the strings 6 characters long.
Here you can Find a Playground which I set up to make some tests.
How can I achieve this in a efficient way?
For any Clarifications on the question, just leave a comment below. Thanks In advance.
atoi(s[,base]) converts a string into an integer. The default is decimal, but you can specify octal 8, hexadecimal 16, or decimal 10.
Python hex() function is used to convert an integer to a lowercase hexadecimal string prefixed with “0x”. We can also pass an object to hex() function, in that case the object must have __index__() function defined that returns integer. The input integer argument can be in any base such as binary, octal etc.
An integer can be converted to a hexadecimal by using the string. ToString() extension method.
import "fmt"
hex := fmt.Sprintf("%06x", num)
The x
means hexadecimal, the 6
means 6 digits, the 0
means left-pad with zeros and the %
starts the whole sequence.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With