Skip to content

Converting Decimal numbers into Binary

2 messages · Rui Barradas, Paul Bernal

#
Hello,

You forgot to cc the list, I'm replying all to have a complete thread so 
that others now and in the future can search similar problems they might 
encounter.
The following function bin2dec works as expected.


bin2dec <- function(x){
   s <- strsplit(x, "")
   s <- lapply(s, function(x){
     sum(as.integer(x)*2^((length(x) - 1):0))
   })
   unlist(s)
}


bin2dec("001100001111010101000011011110")
#[1] 205344990

b <- utf8ToBin("133m at ogP00PD;88MD5MTDww at 2D7k", out = "bin")
bin2dec(b)
# [1]  1  3  3 53 16 55 47 32  0  0 32 20 11  8  8 29 20  5 29 36
#[21] 20 63 63 16  2 20  7 51


Hope this helps,

Rui Barradas

?s 20:52 de 27/12/19, Paul Bernal escreveu:
#
Thank you very much Rui.

Best regards!

El vie., 27 de diciembre de 2019 4:22 p. m., Rui Barradas <
ruipbarradas at sapo.pt> escribi?: