[R] Hexidecimal conversion

Richard A. O'Keefe ok at cs.otago.ac.nz
Fri Dec 3 01:09:27 CET 2004


"Hanke, Alex" <HankeA at mar.dfo-mpo.gc.ca> wrote:
	I can produce the hex(a)decimal equivalent of a decimal number
	but I am having a hard time reversing the operation.

There are bound to be better ways, but perhaps the most obvious method
is
    (1) Break the string into single characters.
    (2) Match them up against the hexadecimal digits.
    (3) Multiply each digit by the appropriate power of 16.
    (4) Add 'em up.

hexdigits <- c("0","1","2","3","4","5","6","7",
               "8","9","A","B","C","D","E","F")

fromhex <- function (s) {
    s <- match(strsplit(s, "")[[1]], hexdigits) - 1
    sum(s * 16 ^ ((length(s)-1) : 0))
}




More information about the R-help mailing list