Print the byte-wise representation of a value
bytes(x, split = TRUE)
bits(x, split = TRUE)https://en.wikipedia.org/wiki/Two's_complement for more
information on the representation used for ints.
https://en.wikipedia.org/wiki/IEEE_floating_point for more
information the floating-point representation used for doubles.
https://en.wikipedia.org/wiki/Character_encoding for an introduction
to character encoding, and ?Encoding for more information on
how R handles character encoding.
## Encoding doesn't change the internal bytes used to represent characters;
## it just changes how they are interpretted!
x <- y <- z <- "\u9b3c"
Encoding(y) <- "bytes"
Encoding(z) <- "latin1"
print(x); print(y); print(z)
#> [1] "鬼"
#> [1] "\\xe9\\xac\\xbc"
#> [1] "鬼"
bytes(x); bytes(y); bytes(z)
#> [1] "E9 AC BC"
#> [1] "E9 AC BC"
#> [1] "E9 AC BC"
bits(x); bits(y); bits(z)
#> [1] "11101001 10101100 10111100"
#> [1] "11101001 10101100 10111100"
#> [1] "11101001 10101100 10111100"
## In R, integers are signed ints. The first bit indicates the sign, but
## values are stored in a two's complement representation. We see that
## NA_integer_ is really just the smallest negative integer that can be
## stored in 4 bytes
bits(NA_integer_)
#> [1] "10000000 00000000 00000000 00000000"
## There are multiple kinds of NAs, NaNs for real numbers
## (at least, on 64bit architectures)
print( c(NA_real_, NA_real_ + 1) )
#> [1] NA NA
rbind( bytes(NA_real_), bytes(NA_real_ + 1) )
#> [,1]
#> [1,] "7F F0 00 00 00 00 07 A2"
#> [2,] "7F F8 00 00 00 00 07 A2"
rbind( bytes(NaN), bytes(0/0) )
#> [,1]
#> [1,] "7F F8 00 00 00 00 00 00"
#> [2,] "FF F8 00 00 00 00 00 00"