[R] Converting decimal to binary in R

(Ted Harding) Ted.Harding at wlandres.net
Sat Dec 14 11:50:06 CET 2013


> On Fri, Dec 13, 2013 at 10:11 PM, 水静流深 <1248283536 at qq.com> wrote:
>> i  have write a function to convert decimal number into binary number in R.
>>
>> dectobin<-function(x){
>>   as.numeric(intToBits(x))->x1
>>   paste(x1,collapse="")->x2
>>   as.numeric(gsub("0+$","",x2))->x3
>>   return(as.character(x3))}
>>
>> dectobin can get right result ,it is so long ,is there a  build-in function
>> to do ?

On 14-Dec-2013 06:17:30 Richard M. Heiberger wrote:
> I recommend
> ?sprintf
> 
> (4^(1/3))^3 != 4
> (4^(1/3))^3 == 4
> (4^(1/3))^3 - 4
> format(c((4^(1/3))^3 , 4), digits=17)
> sprintf("%+13.13a", c((4^(1/3))^3 , 4))

The above generates a hexadecinal representation, not binary!
So it needs further substitutions to get the binary representation.

Can I add a tip which I have very often found useful for this kind
of global substitution? Not just binary -- I first cooked it up
in text-editing when faced with replacing "European" numbers by
"Anglo-Saxon" numbers -- e.g. "1.234.567,89" needs to be converted
into "1,234,567.89", therefore swapping "." and ",". But you don't
want to do "." --> "," and then "," --> "." since you would then
end up with  "1.234.567,89" --> "1,234,567,89" --> "1.234.567.89"

There, the trick was to use a character such as "#", which does
not appear in the text, as a marker for the first substitution while
the second is being made. Then substitute the desired character for "#":
"1.234.567,89" --> "1#234#567,89" --> "1#234#567.89" --> "1,234,567.89"
(first replacing "." by "#", then finally "#" by ",").

You need to replace, for instance, "0" in hex by "0000" in binary,
"1" by "0001", "2" by "0010", ... , "A" by "1010", and so on.
However, you need to avoid replacing already-replaced symbols.

So I suggest using, in a first round, "U" for "1" and "Z" for "0"
(or whatever you prefer, provided it avoids "0" and "1").
So 0 -> ZZZZ, 1 -> ZZZU, ... , A -> UZUZ, etc.
Then, finally, replace each "Z" by "0" and each "U" by "1".

Hence (using a truncated representation), sqrt(pi) in hex is:

  sprintf("%+8.8A", sqrt(pi))
  # [1] "+0X1.C5BF891BP+0"

Then the successive substitutions (which can of course be programmed)
would be:

"+0X1.C5BF891BP+0"

0: "+ZZZZX1.C5BF891BP+ZZZZ"
1: "+ZZZZXZZZU.C5BF89ZZZUBP+ZZZZ"
2: "+ZZZZXZZZU.C5BF89ZZZUBP+ZZZZ"
3: "+ZZZZXZZZU.C5BF89ZZZUBP+ZZZZ"
4: "+ZZZZXZZZU.C5BF89ZZZUBP+ZZZZ"
5: "+ZZZZXZZZU.CZUZUBF89ZZZUBP+ZZZZ"
6: "+ZZZZXZZZU.CZUZUBF89ZZZUBP+ZZZZ"
7: "+ZZZZXZZZU.CZUZUBF89ZZZUBP+ZZZZ"
8: "+ZZZZXZZZU.CZUZUBFUZZZ9ZZZUBP+ZZZZ"
9: "+ZZZZXZZZU.CZUZUBFUZZZUZZUZZZUBP+ZZZZ"
A: "+ZZZZXZZZU.CZUZUBFUZZZUZZUZZZUBP+ZZZZ"
B: "+ZZZZXZZZU.CZUZUUZUUFUZZZUZZUZZZUUZUUP+ZZZZ"
C: "+ZZZZXZZZU.UUZZZUZUUZUUFUZZZUZZUZZZUUZUUP+ZZZZ"
D: "+ZZZZXZZZU.UUZZZUZUUZUUFUZZZUZZUZZZUUZUUP+ZZZZ"
E: "+ZZZZXZZZU.UUZZZUZUUZUUFUZZZUZZUZZZUUZUUP+ZZZZ"
F: "+ZZZZXZZZU.UUZZZUZUUZUUUUUUUZZZUZZUZZZUUZUUP+ZZZZ"

Z: "+0000X000U.UU000U0UU0UUUUUUU000U00U000UU0UUP+0000"
U: "+0000X0001.11000101101111111000100100011011P+0000"

The final result probably needs tidying up in accordance with
the needs of subsequent uses!

Hoping this helps,
Ted.

-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 14-Dec-2013  Time: 10:50:03
This message was sent by XFMail



More information about the R-help mailing list