What would be the easiest way to convert a number to base 2 (in a string, as for example 5 would be converted to "0000000000000101"
) in R? There is intToBits
, but it returns a vector of strings rather than a string:
> intToBits(12)
[1] 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
[26] 00 00 00 00 00 00 00
I have tried some other functions, but had no success:
> toString(intToBits(12))
[1] "00, 00, 01, 01, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00"
intToBits
does not return a vector of strings. It returns a raw vector. Notice the vector has 32 elements. That's one element for each bit (since R uses 32-bit integers). I can't think of a situation where it would be useful to represent a number as a literal string of bits... what are you trying to do? – Planoconvex