Convert hex to decimal in R Convert hex to decimal in R r r

Convert hex to decimal in R


Use base::strtoi to convert hexadecimal character vectors to integer:

strtoi(c("0xff", "077", "123"))#[1] 255  63 123


There is a simple and generic way to convert hex <-> other formats using "C/C++ way":

V <- c(0xa373, 0x115c6, 0xa373, 0x115c6, 0x176b3)sprintf("%d", V)#[1] "41843" "71110" "41843" "71110" "95923"sprintf("%.2f", V)#[1] "41843.00" "71110.00" "41843.00" "71110.00" "95923.00"sprintf("%x", V)#[1] "a373"  "115c6" "a373"  "115c6" "176b3"


As mentioned in @user4221472's answer, strtoi() overflows with integers larger than 2^31.

The simplest way around that is to use as.numeric().

V <- c(0xa373, 0x115c6, 0x176b3, 0x25cf40000)as.numeric(V)#[1]       41843       71110       95923 10149429248