Is there any function to convert binary string into binary or decimal value?
有没有将二进制字符串转换为二进制或十进制值的函数?
If I have a binary string 000101
, what should I do to convert it into 5
?
如果我有一个二进制字符串000101,我该怎么做才能将它转换为5?
5 个解决方案
#1
17
Here is what you can try:
您可以尝试以下方法:
binStr <- "00000001001100110000010110110111" # 20121015
(binNum <- 00000001001100110000010110110111) # 20121015
[1] 1.0011e+24
binVec <- c(1,0,1,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1) # 2670721
shortBin <- 10011010010 # 1234
BinToDec <- function(x)
sum(2^(which(rev(unlist(strsplit(as.character(x), "")) == 1))-1))
BinToDec(binStr)
[1] 20121015
BinToDec(binNum)
[1] 576528
BinToDec(binVec)
[1] 2670721
BinToDec(shortBin)
[1] 1234
That is, you can input both strings (because of as.character()
) and numeric binary values but there are some problems with large numbers like binNum
. As I understand you also want to convert binary string to numeric binary values, but unfortunately there is no such data type at least in base R.
也就是说,您可以输入两个字符串(因为as.character())和数字二进制值,但是像binNum这样的大数字存在一些问题。据我所知,你也想将二进制字符串转换为数字二进制值,但不幸的是,至少在基数R中没有这样的数据类型。
Edit: Now BinToDec
also accepts binary vectors, which might be a solution for large numbers. Function digitsBase()
from package sfsmisc
returns such a vector:
编辑:现在BinToDec也接受二进制向量,这可能是大数字的解决方案。包sfsmisc中的函数digitsBase()返回这样一个向量:
(vec <- digitsBase(5, base= 2, 10))
Class 'basedInt'(base = 2) [1:1]
[,1]
[1,] 0
[2,] 0
[3,] 0
[4,] 0
[5,] 0
[6,] 0
[7,] 0
[8,] 1
[9,] 0
[10,] 1
BinToDec(vec)
[1] 5
Finally, another possibility is package compositions
, for example:
最后,另一种可能性是包装成分,例如:
(x <- unbinary("10101010"))
[1] 170
(y <- binary(x))
[1] "10101010"
#2
24
You could use the packBits
function (in the base
package). Bear in mind that this function requires very specific input.
您可以使用packBits函数(在基础包中)。请记住,此功能需要非常具体的输入。
(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
There is also the strtoi
function (also in the base
package):
还有strtoi函数(也在基础包中):
strtoi("00000001001100110000010110110111", base = 2)
# [1] 20121015
strtoi("000101", base = 2)
# [1] 5
#3
7
base::strtoi(binary_string, base = 2)
#4
6
This function calculates the decimal version with a flexible base. Base equals 2 is binary, etc. This should work up until a base of 10.
此函数使用灵活的基数计算十进制版本。 Base等于2是二进制等。这应该可以工作到10的基数。
base2decimal = function(base_number, base = 2) {
split_base = strsplit(as.character(base_number), split = "")
return(sapply(split_base, function(x) sum(as.numeric(x) * base^(rev(seq_along(x) - 1)))))
}
> base2decimal(c("000101", "00000001001100110000010110110111"))
[1] 5 20121015
#5
0
In the case that you have binary string, all of the prior answers are great. I often find myself in situations where I want to encode a combination of binary vectors. The logic of translating from a combination of 0's and 1's to an integer is always the same:
在你有二进制字符串的情况下,所有先前的答案都很棒。我经常发现自己处于想要编码二进制向量组合的情况。从0和1的组合转换为整数的逻辑总是相同的:
bincount <- function(B, base=2) { return(B %*% base^seq(0,ncol(B)-1)) }
Where B is a matrix, and each column is a binary vector.
其中B是矩阵,每列是二进制矢量。
Example:
例:
isBig <- c(0, 1, 0, 1)
isRed <- c(0, 0, 1, 1)
B = cbind(isBig,isRed)
bincount(B)
# 0 1 2 3
#1
17
Here is what you can try:
您可以尝试以下方法:
binStr <- "00000001001100110000010110110111" # 20121015
(binNum <- 00000001001100110000010110110111) # 20121015
[1] 1.0011e+24
binVec <- c(1,0,1,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1) # 2670721
shortBin <- 10011010010 # 1234
BinToDec <- function(x)
sum(2^(which(rev(unlist(strsplit(as.character(x), "")) == 1))-1))
BinToDec(binStr)
[1] 20121015
BinToDec(binNum)
[1] 576528
BinToDec(binVec)
[1] 2670721
BinToDec(shortBin)
[1] 1234
That is, you can input both strings (because of as.character()
) and numeric binary values but there are some problems with large numbers like binNum
. As I understand you also want to convert binary string to numeric binary values, but unfortunately there is no such data type at least in base R.
也就是说,您可以输入两个字符串(因为as.character())和数字二进制值,但是像binNum这样的大数字存在一些问题。据我所知,你也想将二进制字符串转换为数字二进制值,但不幸的是,至少在基数R中没有这样的数据类型。
Edit: Now BinToDec
also accepts binary vectors, which might be a solution for large numbers. Function digitsBase()
from package sfsmisc
returns such a vector:
编辑:现在BinToDec也接受二进制向量,这可能是大数字的解决方案。包sfsmisc中的函数digitsBase()返回这样一个向量:
(vec <- digitsBase(5, base= 2, 10))
Class 'basedInt'(base = 2) [1:1]
[,1]
[1,] 0
[2,] 0
[3,] 0
[4,] 0
[5,] 0
[6,] 0
[7,] 0
[8,] 1
[9,] 0
[10,] 1
BinToDec(vec)
[1] 5
Finally, another possibility is package compositions
, for example:
最后,另一种可能性是包装成分,例如:
(x <- unbinary("10101010"))
[1] 170
(y <- binary(x))
[1] "10101010"
#2
24
You could use the packBits
function (in the base
package). Bear in mind that this function requires very specific input.
您可以使用packBits函数(在基础包中)。请记住,此功能需要非常具体的输入。
(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
There is also the strtoi
function (also in the base
package):
还有strtoi函数(也在基础包中):
strtoi("00000001001100110000010110110111", base = 2)
# [1] 20121015
strtoi("000101", base = 2)
# [1] 5
#3
7
base::strtoi(binary_string, base = 2)
#4
6
This function calculates the decimal version with a flexible base. Base equals 2 is binary, etc. This should work up until a base of 10.
此函数使用灵活的基数计算十进制版本。 Base等于2是二进制等。这应该可以工作到10的基数。
base2decimal = function(base_number, base = 2) {
split_base = strsplit(as.character(base_number), split = "")
return(sapply(split_base, function(x) sum(as.numeric(x) * base^(rev(seq_along(x) - 1)))))
}
> base2decimal(c("000101", "00000001001100110000010110110111"))
[1] 5 20121015
#5
0
In the case that you have binary string, all of the prior answers are great. I often find myself in situations where I want to encode a combination of binary vectors. The logic of translating from a combination of 0's and 1's to an integer is always the same:
在你有二进制字符串的情况下,所有先前的答案都很棒。我经常发现自己处于想要编码二进制向量组合的情况。从0和1的组合转换为整数的逻辑总是相同的:
bincount <- function(B, base=2) { return(B %*% base^seq(0,ncol(B)-1)) }
Where B is a matrix, and each column is a binary vector.
其中B是矩阵,每列是二进制矢量。
Example:
例:
isBig <- c(0, 1, 0, 1)
isRed <- c(0, 0, 1, 1)
B = cbind(isBig,isRed)
bincount(B)
# 0 1 2 3