有没有人知道 Go 中是否有任何内置功能可以将任何一种数字类型转换为其二进制数字形式。
例如,如果 123
是输入,则字符串 "1111011"
将是输出。
最佳答案
strconv
包有 FormatInt
,它接受 int64
并让您指定基数。
n := int64(123)
fmt.Println(strconv.FormatInt(n, 2)) // 1111011
演示: 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.
关于binary - 从整数转换为其二进制表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13870845/