Function bin2raw was written in order to convert vectors of any type into
vectors containing binary form (little or big endian) of those values
(stored in hex format as 'raw' since it is the only 1-byte type in R). It
was needed in order to convert R vectors to Base64 format commonly used by
XML files. 

It is a great news that next version of R (2.2.0) will extend capabilities
of writeBin function to do the same. If it is more efficient (most likely)
than bin2raw and raw2bin will be retired. In the current version of R the
best solution I could find was too inefficient:

    print(x <- (1:5)*pi)
    writeBin(x, "temp.dat")
    y = readBin("temp.dat", "raw", n = length(x)*8)
    y

 Jarek 
====================================================\==== 
 Jarek Tuszynski, PhD.                           o / \ 
 Science Applications International Corporation  <\__,|  
 (703) 676-4192                                   ">  \ 
 [EMAIL PROTECTED]                     `   \ 



-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 
Sent: Monday, October 03, 2005 11:49 AM
To: Tuszynski, Jaroslaw W.
Cc: Haiyong Xu; Help R
Subject: Re: [R] dec2bin?

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: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] 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,                  [EMAIL PROTECTED]
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

Reply via email to