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", "double"))
    rawShift(x, n)
    numToBits(x)
    numToInt(x)

### 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", "integer", or "double". 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.
- numToBits converts a double precision numeric vector x to a raw vector where each 64-bit double is converted to 64 individual bytes. Each of these bytes contains either 00 or 01, representing the corresponding 0 or 1 bit value of the original double, with the least significant bit first. The first 52 bits are the mantissa, the next 11 bits are the exponent (plus 1023), and the last bit is the sign where double is represented as (1+mantissa/2^52)*2^(exponent-1023)*(-1)^sign.
- numToInts converts a double precision numeric vector x to an integer vector where each 64-bit double is converted to 2 32-bit integers containing the same bit sequence of the double, with the least significant integer first.
- packBits converts the raw, integer, or logical vector x to a raw vector (if type is "raw"), an integer vector (if type is "integer"), or a double precision numeric vector (if type is "double"). The input vector x is divided into groups of 8 (packing to raw), 32 (packing to integer) or 64 (packing to double) 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 bytes, ints, or doubles with the least significant bit first. The length of x must be a multiple of 8 (packing to raw), 32 (packing to integer) or 64 (packing to double). packBits is the inverse of the various <type>ToBits functions.
- 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 8 times the length of x.
intToBits | returns a raw vector 32 times the length of x.
numToBits | returns a raw vector 64 times the length of x.
numToInts | returns an integer vector two times the length of x.
packBits | returns a raw, integer or double precision numeric vector of length length(x)/8, length(x)/32 or length(x)/64, respectively.
rawShift | returns a raw vector of the same length of x.

See Also
raw, Encoding
Examples

    (x <- charToRaw("abc"))
    rawToChar(x)
    rawToChar(x, multiple = TRUE)
    rawToBits(as.raw(0x5))
    intToBits(6)
    packBits(rawToBits(as.raw(c(0x5, 0x7a))))
    packBits(intToBits(c(-17L, 987654321L)), type = "integer")
    rawShift(as.raw(0x03), 1)
    rawShift(as.raw(0x80), -3)
    (x <- numToBits(-1/3))
    packBits(x, "double") # the built-in way
    # and a way that shows the structure of the bits
    ( 1 + (packBits(c(x[1:26],raw(6)), "integer") +
           packBits(c(x[27:52],raw(6)),"integer") * 2^26)/2^52) *
      2 ^ (packBits(c(x[53:63],raw(21)),"integer") - 1023) *
      (-1) ^ as.integer(x[64])
    identical(numToBits(1234.56), intToBits(numToInts(1234.56)))

Package base version 6.1.5-18
Package Index