简体   繁体   中英

Convert an int to hex and then pad it with 0's to get a fixed length String

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM