How to convert hexadecimal to binary in an array in r?
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 am given:
[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 it needs an array.
0
1 answers
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)
1
Author: Rui Barradas, 2018-08-06 21:26:44