我正在尝试将下面显示的十六进制字符向量转换为原始向量
"58" "0a" "00" "00" "00" "02" "00" "03" "02" "00" "00" "02" "03" "00" "00" "00" "03" "13" "00" "00"
我已尝试使用此代码,
as.raw(hexvec)
但是,这给了我以下结果,
3a 00 00 00 00 02 00 03 02 00
Warning messages:
1: NAs introduced by coercion
2: out-of-range values treated as 0 in coercion to raw
我想要的是Raw类型向量中的相同向量(由serialize函数返回)。任何人都可以帮我这个吗?
答案 0 :(得分:1)
您是否尝试为向量中的每个元素应用 charToRaw 函数?
尝试使用此代码?
sapply(hexvec,charToRaw)
答案 1 :(得分:1)
我想这可能会迟到,但是可能会帮助其他人。
hexchar_vector <- c("58", "0a", "00", "00", "00", "02", "00", "03", "02", "00", "00", "02", "03", "00", "00", "00", "03", "13", "00", "00")
integer_vector <- base::strtoi(hexchar_vector, base = 16L) # Convert strings to integers according to the given base using the C function strtol, or choose a suitable base following the C rules.
raw_vector <- base::as.raw(integer_vector) # Creates objects of type "raw" from integers.
这给出了带有管道且没有名称空间引用的情况:
library(magrittr)
hexchar_vector <- c("58", "0a", "00", "00", "00", "02", "00", "03", "02", "00", "00", "02", "03", "00", "00", "00", "03", "13", "00", "00")
raw_vector <- hexchar_vector %>%
strtoi(16L) %>%
as.raw()