[R] dec2bin?

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Oct 3 17:49:16 CEST 2005


On Mon, 3 Oct 2005, Tuszynski, Jaroslaw W. wrote:

> It is unclear what you are trying to do, but check bin2raw in caTools
> package:
>
>>  print(x <- (1:5)*pi)
> [1]  3.141593  6.283185  9.424778 12.566371
> [5] 15.707963
>>         print(y <- bin2raw(x))
> [1] 18 2d 44 54 fb 21 09 40 18 2d 44 54 fb 21 19
> [16] 40 d2 21 33 7f 7c d9 22 40 18 2d 44 54 fb 21
> [31] 29 40 5e 38 55 29 7a 6a 2f 40
>>         print(z <- raw2bin(y,"double"))
> [1]  3.141593  6.283185  9.424778 12.566371
> [5] 15.707963
>
>
> May be that is what you need.

I fail to see what that has to do with the question (there are no `binary 
vectors' in the answer - raw vectors are no more or less binary than 
numeric ones), but in any case it can be done in base R 2.2.0 more 
efficiently by

> options(width=50)
> (y <- writeBin(x, raw()))
  [1] 18 2d 44 54 fb 21 09 40 18 2d 44 54 fb 21 19
[16] 40 d2 21 33 7f 7c d9 22 40 18 2d 44 54 fb 21
[31] 29 40 5e 38 55 29 7a 6a 2f 40


> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Haiyong Xu
> Sent: Saturday, October 01, 2005 4:22 PM
> To: Help R
> Subject: [R] dec2bin?
>
> Hello,
>
> I just want to ask if there is any function that can convert decimal number
> to binary vector.

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