1

For instance, how to convert the number '10010000110000011000011111011000' in Base2 to number in Base4 ?

user2146141
  • 147
  • 1
  • 12

4 Answers4

3

A one-liner approach:

> paste(as.numeric(factor(substring(a,seq(1,nchar(a),2),seq(2,nchar(a),2))))-1,collapse="")
[1] "2100300120133120"

There are multiple ways to split the string into 2 digits, see Chopping a string into a vector of fixed width character elements

fishtank
  • 3,460
  • 1
  • 11
  • 16
1

Here is one approach that breaks up the string into units of length 2 and then looks up the corresponding base 4 for the pair:

convert <- c("00"="0","01"="1","10"="2","11"="3")

from2to4 <- function(s){
  if(nchar(s) %% 2 == 1) s <- paste0('0',s)
  n <- nchar(s)
  bigrams <- sapply(seq(1,n,2),function(i) substr(s,i,i+1))
  digits <- convert[bigrams]
  paste0(digits, collapse = "")
}
John Coleman
  • 46,420
  • 6
  • 44
  • 103
1

Here are a couple inverses:

bin_to_base4 <- function(x){
    x <- strsplit(x, '')
    vapply(x, function(bits){
        bits <- as.integer(bits)
        paste(2 * bits[c(TRUE, FALSE)] + bits[c(FALSE, TRUE)], collapse = '')
    }, character(1))
}

base4_to_bin <- function(x){
    x <- strsplit(x, '')
    vapply(x, function(quats){
        quats <- as.integer(quats)
        paste0(quats %/% 2, quats %% 2, collapse = '')
    }, character(1))
}

x <- '10010000110000011000011111011000'
bin_to_base4(x)
#> [1] "2100300120133120"

base4_to_bin(bin_to_base4(x))
#> [1] "10010000110000011000011111011000"

...and they're vectorized!

base4_to_bin(bin_to_base4(c(x, x)))
#> [1] "10010000110000011000011111011000" "10010000110000011000011111011000"

For actual use, it would be a good idea to put in some sanity checks to ensure the input is actually in the appropriate base.

alistaire
  • 38,696
  • 4
  • 60
  • 94
-4

Convert Base2 to Base10 first, then from Base10 to Base4

user2146141
  • 147
  • 1
  • 12