Skip to content

unsigned 4 byte number

2 messages · tetonedge, William Dunlap

#
Does anybody know how to read in a unsigned 4 byte number from a binary file?
According to the help for readBin, the signed argument only applies to
size=1 or 2. But if you declare any size larger it assumes it is signed?
Thanks
#
R cannot properly represent unsigned 4 byte C ints with its
type "integer" (which is a 4 byte signed C int).  It can store
them as doubles with the desired values.

You can use readBin to read them as signed 4 byte C ints, stored
in R "integers",  with

i <- readBin(file, what="integer", size=4)

and then convert them to doubles with the following function

unsignedFourByteIntToDouble <- function(i) {
   d <- as.numeric(i)
   d[d<0] <- d[d<0] + 2^32
   d
}

If you had S+ you could do this in one step using readBin's
output="double" argument.   In S+'s readBin(), output=type
means to override the default mapping of C types to S+ types.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
p2221555.html