Skip to content
Prev 257650 / 398502 Next

Converting 16-bit to 8-bit encoding?

OK. I'm going to copy this back to R-help too.

With R, we can convert a file of 8-bit integers to 16-bit integers like so:

# Create a test file of 8-bit integers:
con <- file("test.8", "wb")
writeBin(sample(-1L:4L, 1024, TRUE), con, size=1)
close(con)

# Convert test.8 to test.16
icon <- file("test.8", "rb")
ocon <- file("test.16", "wb")
while(length(dat <- readBin(icon, "integer", 1024, size=1)) > 0)
     writeBin(dat, ocon, size=2)
close(icon)
close(ocon)

This assumes (without considering a more formal description of the 
format) that the file and your computing platform agree on how 
multi-byte signed integers are represented.

Hope that will get you going.
On 04/21/2011 11:02 AM, Brian Buma wrote: