[R] binary to decimal number

Uwe Ligges ligges at statistik.uni-dortmund.de
Sat Oct 12 20:09:22 CEST 2002

"E.L. Willighagen" wrote:
> Hi all,
> is there a method for transforming 1010 into 10, i.e. a binary number
> into a decimal value?

I don't have a clever idea right now, but the following should do the
trick, if your binary number is called "b":

 bindec <- function(b) 
   sum(as.integer(unlist(strsplit(b, ""))) * 2^(floor(log10(b)):0))


Uwe Ligges
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list