Skip to content

Instantly share code, notes, and snippets.

@nanxstats
Created October 24, 2012 12:24
Show Gist options
  • Save nanxstats/3945794 to your computer and use it in GitHub Desktop.
Save nanxstats/3945794 to your computer and use it in GitHub Desktop.
R function to convert a string into a vector of its ASCII numbers
ord <- function(x) {
s <- x[1]
intbits <- .Machine$sizeof.long * 4
bits <- raw(nchar(s) * intbits)
r <- charToRaw(x)
idx <- if (.Platform$endian == "little") 1:8 else 25:32
for (i in 1:nchar(s)) {
bits[idx + ((i - 1) * intbits)] <- rawToBits(r[i])
}
packBits(bits, 'integer')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment