从整数转换为二进制表示 [英] Converting from an integer to its binary representation

查看:108
本文介绍了从整数转换为二进制表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道Go中是否有任何内置功能可以将任何一种数字类型转换为其二进制数形式.

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.

例如,如果输入123,则字符串"1111011"将作为输出.

For example, if 123 was the input, the string "1111011" would be the output.

推荐答案

strconv软件包具有FormatInt,该软件包接受int64并允许您指定基数.

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

演示: http://play.golang.org/p /leGVAELMhv

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

func FormatInt(i int64, base int) string

FormatInt返回给定基数中i的字符串表示形式,表示2< = base< =36.结果使用小写字母'a'到'z'表示数字值> =10.

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.

这篇关于从整数转换为二进制表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆