doudu2515 2016-10-27 20:19
浏览 278
已采纳

将int转换为十六进制,然后用0填充以获取固定长度的String

I'm having some issues on trying to convert an int to hex then, padding it with 0s 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 0s, 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.

  • 写回答

1条回答 默认 最新

  • duanba4254 2016-10-27 20:27
    关注
    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?