binary - 从整数转换为其二进制表示

标签 binary go numeric

有没有人知道 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/

相关文章:

PHP MySQL 将二进制字符串保存到 BLOB 字段中无法检索二进制值

go - 我应该明确地创建一个与 "Belongs To"或 "Has Many"对称的关系吗?

go - 关闭 redis 订阅并在 websocket 连接关闭时结束 go 例程

google-app-engine - 如何在 Go 中获取应用程序版本?

java - String.valueOf(i) 与 ""+ i 或 i + ""

list - PackedArray 的快速列表产品标志?

c# - 用于二进制数据的类似 FileHelpers 的数据导入/导出实用程序?

python - 在python中递增二进制数

PHP 十六进制和二进制

c++ - double 的重载比较以允许数值错误