noreferrer> fmt package 获取该整数的字符串表示形式, fmt.Sprintf()%x %X 
  i:= 255 
h:= fmt.Sprintf(%x,i)
fmt.Printf '是'%s'\\\
,i,h)
h = fmt.Sprintf(%X,i)
fmt.Printf('%d'的HEX转换为'%' s'\\\
,i,h)

输出:

 '255'的十六进制转换为'ff'
'255'的HEX转换为'FF'


I want to convert from int to hex in Golang. In strconv, there is a method that converts strings to hex. Is there a similar method to get a hex string from an int?

fmt.Sprintf()%x%X
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'

这篇关于我如何在Golang中将int转换为十六进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!