[R] dec2bin?

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Oct 1 23:44:29 CEST 2005


On Sat, 1 Oct 2005, Haiyong Xu wrote:

> I just want to ask if there is any function that can convert decimal
> number to binary vector.

What do you mean by a binary vector?  Function intToBits might be what you 
are looking for, e.g.

> x <- as.integer(1234)
> y <- intToBits(x)
> options(width=50)
> y
  [1] 00 01 00 00 01 00 01 01 00 00 01 00 00 00 00
[16] 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
[31] 00 00
> z <- paste((0:1)[1+ (rev(y) == 1)], collapse="")
> z
[1] "00000000000000000000010011010010"
> gsub("^0*", "", z)
[1] "10011010010"

Do any of these give what you are looking for?

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list