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

Value
charToRawreturns a raw vector of bytes.
rawToCharreturns a single character string or a character vector of single multiple individual characters.
rawToBitsreturns a raw vector 8 times the length of x.
intToBitsreturns a raw vector 32 times the length of x.
numToBitsreturns a raw vector 64 times the length of x.
numToIntsreturns an integer vector two times the length of x.
packBitsreturns a raw, integer or double precision numeric vector of length length(x)/8, length(x)/32 or length(x)/64, respectively.
rawShiftreturns 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.0.0-69
Package Index