如何在R中读取整个二进制blob?
我试图读入二进制文件的全部内容。类似于以下Python代码:如何在R中读取整个二进制blob?,r,R,我试图读入二进制文件的全部内容。类似于以下Python代码: with open("Male_Moose.jpg") as f: data = f.read() print "Length of file: ", len(data) 这是我的R代码: main <- function() { fname <- "Male_Moose.jpg" contents <- readBin(fname, file.info(fname)$size) cat(p
with open("Male_Moose.jpg") as f:
data = f.read()
print "Length of file: ", len(data)
这是我的R代码:
main <- function()
{
fname <- "Male_Moose.jpg"
contents <- readBin(fname, file.info(fname)$size)
cat(paste("File size: ", nchar(contents, type = "bytes")))
cat("\n\n")
cat(paste("File info size:", file.info(fname)$size))
cat("\n\n")
}
main()
您可能会发现使用jpeg软件包更容易
library(jpeg)
MooseImage = readJPEG("Male_Moose.jpg")
如果你只想要一滴
MooseBlob = as.vector(MooseImage)
您遗漏了
readBin
的第二个参数,这是您要读取的数据类型:
readBin(con, what, n = 1L, size = NA_integer_, signed = TRUE,
endian = .Platform$endian)
指定将数据作为原始字节向量读取的“原始”:
contents <- readBin(fname, "raw", file.info(fname)$size)
length(contents) # not nchar()
内容您能提供更多的证据吗?file.info(fname)$size
返回什么?那么nchar(contents,type=“bytes”)
呢?使用readBin
时,默认数据类型也是“integer”。是否要mode=“raw”
读取字节?尝试阅读?readBin
帮助页面。您好,Flick,我之所以要读取整个二进制文件,是因为我想计算该文件的哈希值,并将其填充到哈希映射中。
contents <- readBin(fname, "raw", file.info(fname)$size)
length(contents) # not nchar()