bin2raw {caMassClass} | R Documentation |
Convert R vectors of any type to and from the raw binary format, stored as vector of type "raw".
r = bin2raw(x, size=NA) x = raw2bin(r, what, size=NA, signed = TRUE)
x |
vector or any structure that can be converted to a vector by
as.vector function. Strings are also allowed. |
r |
vector of type "raw" |
what |
Either an object whose mode will give the mode of the vector
to be created, or a character vector of length one describing
the mode: one of '"numeric", "double", "integer", "int",
"logical", "complex", "character", "raw". Same as variable
what in readBin and base64decode
functions. |
size |
integer. The number of bytes per element in the byte stream
stored in r . The default, 'NA ', uses the natural size. See
details. |
signed |
logical. Only used for integers of sizes 1 and 2, when it determines if the quantity stored as raw should be regarded as a signed or unsigned integer. |
Quoting from readBin
documentation:
"If 'size' is specified and not the natural size of the object, each element of the vector is coerced to an appropriate type before being written or as it is read. Possible sizes are 1, 2, 4 and possibly 8 for integer or logical vectors, and 4, 8 and possibly 12/16 for numeric vectors. (Note that coercion occurs as signed types except if 'signed = FALSE' when reading integers of sizes 1 and 2.) Changing sizes is unlikely to preserve 'NA's, and the extended precision sizes are unlikely to be portable across platforms."
Function bin2raw
returns vector of raw values (see r
above), where each 1-byte raw value correspond to 1-byte of 1-byte of the
binary form of other types. Length of the vector is going to be "number of
bytes of a single element in array x
" times length(x)
.
Function raw2bin
returns vector of appropriate mode
and length (see x
above), where each 1-byte raw value correspond to
1-byte of the binary form of other types. Length of the vector is going to
be number of bytes per element in array x
times length(x)
.
If parameter what
is equal to "character" than a string (of length 1)
is returned instead of vector f characters.
At the moment those two functions use calls to writeBin
and readBin
functions to do the job. I hope to change it in
the future by writing a C code.
Jarek Tuszynski (SAIC) jaroslaw.w.tuszynski@saic.com
x = (10*runif(10)>5) # logical for (i in c(NA, 1, 2, 4)) { y = bin2raw(x, size=i) z = raw2bin(y,typeof(x), size=i) stopifnot(x==z) } print("Checked bin2raw and raw2bin conversion for logical type") x = as.integer(1:10) # integer for (i in c(NA, 1, 2, 4)) { y = bin2raw(x, size=i) z = raw2bin(y,typeof(x), size=i) stopifnot(x==z) } print("Checked bin2raw and raw2bin conversion for integer type") x = (1:10)*pi # double for (i in c(NA, 4, 8)) { y = bin2raw(x, size=i) z = raw2bin(y,typeof(x), size=i) stopifnot(mean(abs(x-z))<1e-5) } print("Checked bin2raw and raw2bin conversion for double type") x = log(as.complex(-(1:10)*pi)) # complex y = bin2raw(x) z = raw2bin(y,typeof(x)) stopifnot(x==z) print("Checked bin2raw and raw2bin conversion for complex type") x = "Chance favors the prepared mind" # character y = bin2raw(x) z = raw2bin(y,typeof(x)) stopifnot(x==z) print("Checked bin2raw and raw2bin conversion for character type")