How can I convert from int to hex

前端 未结 6 2033
你的背包
你的背包 2020-12-14 15:53

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?

6条回答
  •  生来不讨喜
    2020-12-14 16:37

    "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

提交回复
热议问题