How can I convert from int to hex

huangapple go评论86阅读模式
英文:

How can I convert from int to hex

问题

我想在Golang中将整数转换为十六进制。
在strconv包中,有一个方法可以将字符串转换为十六进制。是否有类似的方法可以从整数获取十六进制字符串?

英文:

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?

答案1

得分: 65

由于十六进制是一个整数字面量,你可以使用fmt.Sprintf()函数和%x%X格式来请求该整数的字符串表示。以下是示例代码和输出结果:

i := 255
h := fmt.Sprintf("%x", i)
fmt.Printf("Hex conv of '255' is '%s'\n", h)
h = fmt.Sprintf("%X", i)
fmt.Printf("HEX conv of '255' is '%s'\n", h)

输出结果:

Hex conv of '255' is 'ff'
HEX conv of '255' is 'FF'

你可以在这里查看示例代码的运行结果。

英文:

Since hex is a Integer literal, you can ask the fmt package for a string representation of that integer, using fmt.Sprintf(), and the %x or %X format.
See playground

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'

答案2

得分: 44

"Hex"不是一个真实存在的东西。你可以使用一个十六进制表示的数字,但是0xFF255之间没有任何区别。关于这个问题的更多信息可以在文档中找到,文档指出你可以使用0xff来定义一个整数常量255!正如你提到的,如果你想找到一个整数的十六进制表示,你可以使用strconv

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(strconv.FormatInt(255, 16))
    // 输出 "ff"
}

在 playground 中试一试

英文:

"Hex" isn't a real thing. You can use a hexadecimal representation of a number, but there's no difference between 0xFF and 255. More info on that can be found in the docs which point out you can use 0xff to define an integer constant 255! As you mention, if you're trying to find the hexadecimal representation of an integer you could use strconv

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(strconv.FormatInt(255, 16))
    // gives "ff"
}

Try it in the playground

答案3

得分: 28

如果要格式化一些字节,十六进制需要用两位数字表示,并在前面加上0。

例如:1 => '01',15 => '0f',等等。

可以通过以下方式强制Sprintf遵循这个规则:

h := fmt.Sprintf("%02x", 14)
fmt.Println(h) // 0e
h2 := fmt.Sprintf("%02x", 231)
fmt.Println(h2) // e7

模式"%02x"的含义是:

  • '0' 强制使用零填充
  • '2' 设置输出大小为两个字符
  • 'x' 转换为十六进制
英文:

If formatting some bytes, hex needs a 2 digits representation, with leading 0.

For exemple: 1 => '01', 15 => '0f', etc.

It is possible to force Sprintf to respect this :

h:= fmt.Sprintf("%02x", 14)
fmt.Println(h) // 0e
h2:= fmt.Sprintf("%02x", 231)
fmt.Println(h2) // e7

The pattern "%02x" means:

  • '0' force using zeros
  • '2' set the output size as two charactes
  • 'x' to convert in hexadecimal

答案4

得分: 3

以下是翻译好的内容:

i := 4357640193405743614
h := fmt.Sprintf("%016x", i)
fmt.Printf("Decimal: %d,\nHexa: %s", i, h)

# 结果
Decimal: 4357640193405743614,
Hexa: 3c7972ab0ae9f1fe

Playground: https://play.golang.org/p/ndlMyBdQjmT

请注意,我只翻译了代码部分,其他内容不做翻译。

英文:
i := 4357640193405743614
h := fmt.Sprintf("%016x",i)
fmt.Printf("Decimal: %d,\nHexa: %s", i, h)

# Result
Decimal..: 4357640193405743614,
Hexa.....: 3c7972ab0ae9f1fe

Playground: https://play.golang.org/p/ndlMyBdQjmT

答案5

得分: 2

Sprintf更加灵活,但FormatInt更快。选择适合你的方法。

func Benchmark_sprintf(b *testing.B) { // 83.8 ns/op
    for n := 0; n < b.N; n++ {
        _ = fmt.Sprintf("%x", n)
    }
}
func Benchmark_formatint(b *testing.B) { // 28.5 ns/op
    bn := int64(b.N)
    for n := int64(0); n < bn; n++ {
        _ = strconv.FormatInt(n, 16)
    }
}
英文:

Sprintf is more versatile but FormatInt is faster. Choose what is better for you

func Benchmark_sprintf(b *testing.B) { // 83.8 ns/op
	for n := 0; n &lt; b.N; n++ {
		_ = fmt.Sprintf(&quot;%x&quot;, n)
	}
}
func Benchmark_formatint(b *testing.B) { // 28.5 ns/op
	bn := int64(b.N)
	for n := int64(0); n &lt; bn; n++ {
		_ = strconv.FormatInt(n, 16)
	}
}

答案6

得分: 1

如果它是uint32类型,你可以按照下面的方式将其转换为十六进制:

var p uint32
p = 4278190335
r := p >> 24 & 0xFF
g := p >> 16 & 0xFF
b := p >> 8 & 0xFF
fmt.Println(r, g, b) //255 0 0

你也可以使用这个在线工具进行参考:https://cryptii.com/pipes/integer-encoder

英文:

E.g. if its uint32, you can convert it to HEX as seen below =>

    var p uint32
	p = 4278190335
	r := p &gt;&gt; 24 &amp; 0xFF
	g := p &gt;&gt; 16 &amp; 0xFF
	b := p &gt;&gt; 8 &amp; 0xFF
	fmt.Println(r, g, b)//255 0 0

DEMO

you can also check this online tool for ref. https://cryptii.com/pipes/integer-encoder

huangapple
  • 本文由 发表于 2015年11月7日 17:59:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/33581426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定