Displaying 2 results from an estimated 2 matches for "bindec".
Did you mean:
binded
2002 Oct 14
4
log10(), floor() combo issue?
Hi all,
in my search for a nice binary2decimal method, I received this nice
code (thanx to Uwe Ligges):
bindec <- function(b)
sum(as.integer(unlist(strsplit(b, ""))) * 2^(floor(log10(b)):0))
It fails, however, with:
> bindec(1000)
[1] 4
Warning message:
longer object length
is not a multiple of shorter object length in:
as.integer(unlist(strsplit(b, ""))) * 2^(floor(l...
2002 Oct 15
1
X-Priority: 3 (Normal)
# you need to distinguish datatypes 'character' and 'numeric'
bindec <- function(
b # a CHARACTER representing a binary number
){
as.i <- as.integer(unlist(strsplit(b,"")))
print(as.i)
fl <- 2^(floor(round(log10(as.numeric(b)),10)):0) # convert b to numeric
print(fl)
dec <- sum(as.i * fl)
dec
}
> bindec("100000")
[1] 1 0 0 0 0 0...