我正在尝试将下面所示的十六进制字符向量转换为原始向量,
"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
我想要的是原始类型向量中的相同向量(由序列化函数返回)。谁能帮我解决这个问题吗?
最佳答案
我想现在来 awser 已经太晚了,但它可能对其他人有帮助。
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()
关于r - 将十六进制字符向量转换为 R 中的原始向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40673592/