将二进制字符串转换为二进制或十进制值 [英] Convert binary string to binary or decimal value

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

问题描述

有没有函数可以将二进制字符串转换为二进制或十进制值?

Is there any function to convert binary string into binary or decimal value?

如果我有一个二进制字符串000101,我应该怎么做才能把它转换成5?

If I have a binary string 000101, what should I do to convert it into 5?

推荐答案

您可以使用 packBits 函数(在 base 包中).请记住,此功能需要非常具体的输入.

You could use the packBits function (in the base package). Bear in mind that this function requires very specific input.

(yy <- intToBits(5))
#  [1] 01 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
# [26] 00 00 00 00 00 00 00
# Note that there are 32 bits and the order is reversed from your example

class(yy)
[1] "raw"

packBits(yy, "integer")
# [1] 5

还有strtoi函数(也在base包中):

There is also the strtoi function (also in the base package):

strtoi("00000001001100110000010110110111", base = 2)
# [1] 20121015

strtoi("000101", base = 2)
# [1] 5

这篇关于将二进制字符串转换为二进制或十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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