R/08-bit-to-raw.R

Defines functions uint8_to_raw int8_to_raw hex8_to_raw chr_to_raw utf8_to_raw

Documented in chr_to_raw hex8_to_raw int8_to_raw uint8_to_raw utf8_to_raw

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @rdname raw_to_raw
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
uint8_to_raw <- function(uint8_vec, ...) {
  stopifnot(is_uint8_vec(uint8_vec))

  as.raw(uint8_vec)
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @rdname raw_to_raw
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int8_to_raw <- function(int8_vec, ...) {
  stopifnot(is_int8_vec(int8_vec))

  writeBin(int8_vec, raw(), size = 1L, useBytes = TRUE)
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @rdname raw_to_raw
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
hex8_to_raw <- function(hex8_vec, ...) {
  stopifnot(is_hex8_vec(hex8_vec))
  as.raw(strtoi(hex8_vec, base = 16L))
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @rdname raw_to_raw
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
chr_to_raw <- function(chr_vec, ...) {
  stopifnot(is_chr_vec(chr_vec))

  charToRaw(chr_vec)
}


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' @rdname raw_to_raw
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
utf8_to_raw <- function(utf8_vec, ...) {
  stopifnot(is_utf8_vec(utf8_vec))

  as.raw(utf8ToInt(utf8_vec))
}
coolbutuseless/minitypes documentation built on May 27, 2019, 9:55 a.m.