# 0x00, 0x7f, 0x80, 0xFF
x <- as.raw(c(0, 127, 128, 255))
raw_to_uint8(x)
# The first bit becomes the integer sign
# 128 -> -128, 255 -> -1
raw_to_int8(x)
## Comments based on little endian system
# 0x7f00 (32512), 0xFF80 (65408 unsigned, or -128 signed)
raw_to_uint16(x)
raw_to_int16(x)
# 0xFF807F00 (4286611200 unsigned, -8356096 signed)
raw_to_uint32(x)
raw_to_int32(x)
# ---------------------------- String ---------------------------
# ASCII case: all valid
x <- charToRaw("This is an ASCII string")
raw_to_string(x)
rawToChar(x)
x <- c(charToRaw("This is the end."),
as.raw(0),
charToRaw("*** is invalid"))
# rawToChar will raise error
raw_to_string(x)
# ---------------------------- Integer64 ------------------------
# Runs on little endian system
x <- as.raw(c(0x80, 0x00, 0x7f, 0x80, 0xFF, 0x50, 0x7f, 0x00))
# Calculate bitstring, which concaternates the followings
# 10000000 (0x80), 00000000 (0x00), 01111111 (0x7f), 10000000 (0x80),
# 11111111 (0xFF), 01010000 (0x50), 01111111 (0x7f), 00000000 (0x00)
if(.Platform$endian == "little") {
bitstring <- paste0(
"00000000011111110101000011111111",
"10000000011111110000000010000000"
)
} else {
bitstring <- paste0(
"00000001000000001111111000000001",
"11111111000010101111111000000000"
)
}
# This is expected value
bit64::as.integer64(structure(
bitstring,
class = "bitstring"
))
# This is actual value
raw_to_int64(x)
Run the code above in your browser using DataLab