r - 将十六进制字符向量转换为 R 中的原始向量

标签 r casting type-conversion

我正在尝试将下面所示的十六进制字符向量转换为原始向量,

"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/

相关文章:

正则表达式(使用 tidyr 收集多组列)

r - 有没有办法导出错误信息?

将一列重新排列成两列数据框 r

java - 将整数类型分配给 int

c++ - 将 boost::asio::ip::address_v4 隐藏为字符串

r - 根据年份和类别计算百分比差异

c++ - 将 char 数组结构 vector 转换为 POD vector ?

c++ - 传递接受 void * 的成员函数

c++ - Visual Studio 2017 : How to make intellisense accept friend conversion constructor from other class?

c++ - lambda 的参数和返回值的类型转换规则是什么?