in reply to Base10 to Base2.
This simply splits a decimal string into a list of characters, then reverses them to preserve order. The map statement is performed on all values in the list in order: the list moves leftward, incrementing by powers of 2, and adding the increment counter to an accumulator variable if the current digit is 1. This doesn't perform any checking however, you asked how to convert from decimal to binary, and this does it. Any non-zero-or-one value will *work*, but will not produce the expected value.my ($acc,$digit_count,$dec_string); $digit_count = 1; $dec_string = "1101110"; map {$acc += $_*$digit_count;$digit_count *=2;} reverse split '',$dec_ +string; print $acc;
|
|---|