我想在 Golang 中将 int 转换为 hex。
在strconv中,有一个将字符串转换为十六进制的方法。是否有类似的方法从 int 获取十六进制字符串?
由于十六进制是整数文字 https://golang.org/ref/spec#Integer_literals,你可以询问FMMT包 https://golang.org/pkg/fmt/对于该整数的字符串表示形式,使用fmt.Sprintf() https://golang.org/pkg/fmt/#Sprintf,以及%x
or %X
format.
查看游乐场 http://play.golang.org/p/GXleKw0OiF
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)
Output:
Hex conv of '255' is 'ff'
HEX conv of '255' is 'FF'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)