简体   繁体   中英

How to convert hexadecimal to binary in a matrix in r?

I have an matrix with hexadecimal number, like that:

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

If I try convert to binary, with hex2bin(matrix) (library BMS) give me:

  [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 need a matrix.

Maybe like this? I convert to decimal integer using the built-in strtoi , then take cues from Converting decimal to binary for getting to binary.

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

m[] = R.utils::intToBin(strtoi(m, base = 16L))
m
#      [,1]       [,2]       [,3]      
# [1,] "11111010" "11001110" "00001110"
# [2,] "11111000" "00010111" "11010110"
# [3,] "11010000" "01101010" "00100010"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM