in reply to Reading Binary
Given some input string representing a binary number, $bin, verify it only contains 1 & 0, to protect little Bobby Tables, then convert to an ordinary number. You only have 15 values to deal with, so you can hardcode an array. If it's 10 to 20 bits, you can precompute the array; if it's more, probably better to stick with calculating things as you need them, possibly with memoizing.
my @block = qw( D C CD B BD BC BCD A AD AC ACD AB ABD ABC ABCD ); unshift @block, ''; # empty string for 0 $bin =~ m{([01]+)}; # untaint input my $idx = eval "0b$1"; my $result = $block[$idx];
As Occam said: Entia non sunt multiplicanda praeter necessitatem.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Reading Binary
by jwkrahn (Abbot) on Jun 15, 2011 at 20:47 UTC |