【发布时间】:2020-10-25 09:29:57
【问题描述】:
我正在读取 R 中的二进制文件,需要读取 10 个字节,必须将其解释为 4 位无符号整数(每个字节 2 个,所以我猜是 0..15 范围内的 20 个值)。
根据我对文档的理解,这不能直接使用 readBin 完成,因为要读取的最小长度 1 表示 1 字节。
所以我认为我需要将数据读取为 1 字节整数并使用按位运算来获取 4 位整数。我发现这些值在 R 内部存储为 32 位整数,我发现 this explanation on SO 似乎描述了我想要做的事情。所以这是我对遵循建议的 R 函数的尝试:
#' @title Interprete bits start_index to stop_index of input int8 as unsigned integer.
uint8bits <- function(int8, start_index, stop_index) {
num_bits = stop_index - start_index + 1L;
bitmask = bitwShiftL((bitwShiftL(1L, num_bits) -1L), stop_index);
return(bitwShiftR(bitwAnd(int8, bitmask), start_index));
}
但是,它不能按预期工作,例如,要从读取值中取出两个数字(本例中为 255),我将调用该函数一次以提取位 1 到 4,然后再调用一次以提取位 5到 8 点:
value1 = uint8bits(255L, 1, 4); # I would expect 15, but the output is 120.
value2 = uint8bits(255L, 5, 8); # I would expect 15, but the output is 0.
我做错了什么?
【问题讨论】:
-
您的二进制文件是如何存储的?你能用另一种语言展示你想要什么吗?
readBin(<file>)返回什么? -
它们存储为小端,如果这就是你的意思。我认为它在链接线程中针对 C 进行了演示。 documentation for readbin is here,当我把它称为
val = readBin(myfilehandle, integer(), n=1, size=1, endian='little', signed = FALSE)时,val 是一个 0..255 范围内的整数值。但是 R 在内部使用 32 位整数,所以它不存储为 8 位整数。
标签: r bit-manipulation