has a problem. Anything matched by /^[0-7]+$/
is going to be
matched by /^(0x)?[0-9A-Fa-f]+$/i
as well, so if you
do the tests in order you present it, it'll assume either hexadecimal, or binary. Reversing the order will give a problem as well. How would you distinguish between 1016 == 1610
, and 108 == 810
(subscripts indicate base)?
Of course, as pointed out, your fallacy lies in assuming that oct and hex return decimal representations of numbers - they don't. They return numbers:
$ perl -MDevel::Peek -wle 'Dump hex 10'
SV = IV(0x8192014) at 0x8181270
REFCNT = 1
FLAGS = (IOK,READONLY,pIOK)
IV = 16
It's a number - without a stringified valued.
Also, Perl already has a function to turn a number into a hexadecimal, octal or binary representation: it's called sprintf.
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:
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
| & || & |
| < || < |
| > || > |
| [ || [ |
| ] || ] ||