How to convert hexadecimal to binary in an array in r?

0

I have an array with hexadecimal number, like this:

     [,1] [,2] [,3]
[1,] "FA" "F8" "D0"
[2,] "CE" "17" "6A"
[3,] "0E" "D6" "22"

If I try to convert to binary, with hex2bin(matriz) (biblioteca BMS) , I get:

  [1] 1 1 1 1 1 0 1 0 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 0 1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 0
 [62] 0 0 1 0 1 1 1 0 0 0 0 1 1 0 1 0 1 1 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 0 0 0 0 0
[123] 1 0 0 0 1 0 0 0 0 0 1 1 1 1 1 0 1 0 0 0 0 0 1 0 0 1 0 1 1 0 0 0 0 0 0 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0
[184] 1 0 0 0 1

But you need an array.

    
asked by anonymous 06.08.2018 / 22:16

1 answer

1

The following function might do what you want. First the data.

m <- matrix(c("FA", "F8", "D0", "CE", "17", "6A", "0E", "D6", "22"),
            nrow = 3, byrow = TRUE)

Now the code.

library(BMS)

hex2matrix <- function(M){
  R <- NULL
  for(i in seq_len(ncol(M))){
    B <- sapply(M[, i], hex2bin)
    R <- cbind(R, B)
  }
  R
}


hex2matrix(m)
    
06.08.2018 / 23:26