It looks like strtoi cannot handle numbers larger than 2^31 :
strtoi("1111111111111111111111111111111", base=2L) # [1] 2147483647 strtoi("10000000000000000000000000000000", base=2L) # [1] NA
The maximum integer that my machine (and probably yours) can process for an integer:
.Machine$integer.max
Note that the documentation warns of overflow (from ?strtoi ):
Values ββthat cannot be interpreted as integers or overflows are returned as NA_integer_ .
What you can do is write your own function, which returns the output as a number instead of an integer:
convert <- function(x) { y <- as.numeric(strsplit(x, "")[[1]]) sum(y * 2^rev((seq_along(y)-1))) } convert("1111111111111111111111111111111")
source share