Converting from an integer to its binary representation

前端 未结 7 1464
野的像风
野的像风 2020-12-24 00:09

Has anyone got an idea if there is any inbuilt functionality in Go for converting from any one of the numeric types to its binary number form.

For example, if

7条回答
  •  难免孤独
    2020-12-24 00:31

    The strconv package has FormatInt, which accepts an int64 and lets you specify the base.

    n := int64(123)
    
    fmt.Println(strconv.FormatInt(n, 2)) // 1111011
    

    DEMO: http://play.golang.org/p/leGVAELMhv

    http://golang.org/pkg/strconv/#FormatInt

    func FormatInt(i int64, base int) string

    FormatInt returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z' for digit values >= 10.

提交回复
热议问题