rawConversion
Convert to or from Raw Vectors
Description
Utilities for raw vectors conversion, shifting, and packing.
Usage
charToRaw(x)
rawToChar(x, multiple = FALSE)
rawToBits(x)
intToBits(x)
packBits(x, type = c("raw", "integer"))
rawShift(x, n)
Arguments
x |
a vector of data to convert, shift, or pack.
See Details for its meaning in different functions.
|
multiple |
a logical value. If TRUE, the conversion should be to
multiple individual characters. If FALSE, the conversion
should be to a single character string.
|
n |
the number of bits to shift.
|
type |
the result type of packing.
Can be "raw" or "integer".
Partial character matching is allowed.
|
Details
- charToRaw converts a character string x or the first
element of a character vector x to a raw vector of bytes.
It accesses the actual bytes of the string, ignoring the string encoding.
- rawToChar converts a raw vector x to
a single character string (if multiple is FALSE)
or a character vector of multiple individual characters
(if multiple is TRUE).
The resulting strings all have encoding "unknown".
- rawToBits converts raw vector x to a raw vector where
each 8-bit byte is converted to eight individual bytes. Each of these
bytes contains either 00 or 01, representing the corresponding 0 or 1
bit value of the original byte, with the least significant bit first.
- intToBits converts integer vector x to a raw vector
where each 32-bit integer is converted to 32 individual bytes. Each
of these bytes contains either 00 or 01, representing the
corresponding 0 or 1 bit value of the original integer, with the least
significant bit first.
- packBits converts input raw, integer or logical vector x
to a raw vector (if type is "raw") or integer vector (if
type is "integer"). The input vector x is
divided into groups of 8 (packing to raw) or 32 (packing to integer)
bit values, where the least significant bit of each element determines
whether the corresponding bit of the output is 0 or 1. The bit values
are packed into byte or integers with the least significant bit first.
The length of x must be a multiple of 8 (packing to raw) or 32
(packing to integer).
- rawShift shifts the bits in each byte in raw vector x
the specified number of bits n. The n must be an
integer between -8 to 8 inclusive. Positive n values shift
each byte towards the most significant bits and negative n
values shift towards the least significant bits.
Value
charToRaw | returns a raw vector of bytes. |
rawToChar | returns a single character string or a character
vector of single multiple individual characters. |
rawToBits | returns a raw vector of 8 times length of x. |
intToBits | returns a raw vector of 32 times length of x. |
packBits | returns a raw or integer vector of length
length(x)/8 or length(x)/32. |
rawShift | returns a raw vector of the same length of x. |
See Also
Examples
x <- charToRaw("abc")
x
# prints [1] 61 62 63
rawToChar(x)
# returns "abc"
rawToChar(x, multiple = TRUE)
# returns c("a","b","c")
rawToBits(as.raw(0x5))
# [1] 01 00 01 00 00 00 00 00
intToBits(6)
# [1] 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00
# [17] 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
packBits(as.raw(1:3))
# Error: argument 'x' must be a multiple of 8 long
packBits(as.raw(1:8))
# [1] 55
packBits(as.raw(1:32),"integer")
# [1] 1431655765
rawShift(as.raw(0x03), 1)
# [1] 06
rawShift(as.raw(0x80), -3)
# [1] 10