Sam*_*ami 5 binary parsing hex r
我有一个文件,其中包含原始/二进制数据和ASCII。它包含一个时间戳和一个代表速度的无符号 16 位值。我想用“R”绘制该速度,因此读取该文件。
该文件的定义是:
从午夜开始的毫秒数 - In ascii
+ ':$'
+ BufferStr
+ #13#10
然后 BufferString 包含我想提取的以下信息:
字节 3:序列字(无符号)
字节 4 和 5:速度(无符号)
...
我想生成一个包含 2 列时间和速度的表
我的方法是将文件作为 csv 读取一次以获取时间戳列。
library(bit)
dataPath="rawdata.txt"
#to create time col read file as csv:
rwch=read.csv(dataPath, sep=":")
Run Code Online (Sandbox Code Playgroud)
然后再次以二进制形式读取文件。我将文件分成 27 字节的部分,这是一行的长度。
#read binary
to.read = file(path, "rb")
rw=readBin(to.read, what="raw", n = 100*27, endian = "big")
rws=split(rw, ceiling(seq_along(rw)/27))
Run Code Online (Sandbox Code Playgroud)
但后来我卡住了。结果向量不包含原始数据,我无法再次拆分它以获取速度。我尝试了几个函数,例如 hexView ,readRaw 但我无法生成我的列表。
我很高兴有任何提示
最好在正在处理的连接上使用 readBin(),而不是尝试读取整个文件(除非文件中只有一种 data.type,但这里有混合类型)。这似乎适用于您的示例文件。
N<-28
RC<-27
secs<-numeric(N)
speeds<-numeric(N)
con<-file("/rawdata.txt", "rb")
for(i in seq.int(N)) {
print(i)
secs[i] <- as.numeric(readChar(con,8))
stopifnot(readChar(con,2)==":$") #check
readBin(con,"raw",3) #skip 3 bytes
speeds[i] <- readBin(con, "int",1,2, signed=F)
readBin(con,"raw",10) #skip 10 bytes
stopifnot(readBin(con,"raw",2)==c(13,10)) #check
}
data.frame(secs,speeds)
close(con)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4965 次 |
| 最近记录: |