Re: [R] dec2bin?

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Sun 02 Oct 2005 - 07:44:29 EST

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@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

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Sun Oct 02 07:52:44 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 18:08:15 EST