# hexadecimal number system
hexadecimal <- 0x42
# hexadecimal = 66
# Trap: no octal literal, 042 is plain decimal
octal_literal <- 042
# octal_literal = 42
# Octal and binary come from strings
octal <- strtoi("42", base = 8L)
# octal = 34
binary <- strtoi("1010", base = 2L)
# binary = 10
# 42 to octal string
s_octal <- sprintf("%o", 42L)
# s_octal = 52
# 42 to hexadecimal string
s_hexadecimal <- sprintf("%x", 42L)
# s_hexadecimal = 2a
# 42 to binary string: 32 bits, leading 0 cut
bits <- rev(as.integer(intToBits(42L)))
s_binary <- sub("^0+", "",
paste(bits, collapse = ""))
# s_binary = 101010
# String in any base back to int
from_hexadecimal <- strtoi("2a", base = 16L)
# from_hexadecimal = 42
cat("hexadecimal =", hexadecimal, "\n")
cat("octal_literal =", octal_literal, "\n")
cat("octal =", octal, "\n")
cat("binary =", binary, "\n")
cat("s_octal =", s_octal, "\n")
cat("s_hexadecimal =", s_hexadecimal, "\n")
cat("s_binary =", s_binary, "\n")
cat("from_hexadecimal =", from_hexadecimal, "\n")