将二进制字符串转换为二进制或十进制值

编程入门 行业动态 更新时间:2024-10-27 03:40:25
本文介绍了将二进制字符串转换为二进制或十进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

更多推荐

将二进制字符串转换为二进制或十进制值

本文发布于:2023-06-05 01:21:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/508970.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   字符串   十进制值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!