If you want to know how conversion of numbers to a string works, try this:
$num = 1234567;
$result = "";
$base = 2;
do {
use integer;
my $digit = $num % $base;
substr($result, 0, 0) = $digit; # for any number < 10
$num /= $base;
} while $num;
print $result;
If you like fixed width output, add enough zeros to the front, afterwards:
substr($result, 0, 0) = "0" while length($result) < 32;
If you want to play with $base > 10, for example hexadecimal , try:
$num = 1234567;
$base = 16;
$result = "";
my @digit = (0 .. 9, 'A' .. 'Z');
do {
use integer;
my $digit = $num % $base;
substr($result, 0, 0) = $digit[$digit]; # for base up to 36
$num /= $base;
} while $num;
print $result;
Note that the do { ... } while $num; construct makes the loop execute at least once, so you get a proper result for zero.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.