Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I've often felt that base manipulation was one of perl's shortcomings -- not because it can't do it just because it's difficult and, as you point out, the functions work in reverse.

I don't want to see your magic function because we'd then have:

dec(998) == 998 dec(999) == 999 dec(1000) == 8 dec(1001) == 9 dec(1002) == 514
which I'm sure you'll agree is just wrong.

I'd prefer that the existing functions did what they appear to do: hex() returns a hexidecimal representation of the input data and oct() returns an octal representation. Similarly a bin() function should return a binary representation.

As changing now would be an absolute backward-compatability nightmare, I'd suggest using the full names for decimal-to-base-n conversion: hexidecimal(255) eq 'FF'.

We might also include generic base manipulator, but it's probably more a loadable (module) function (the functionality below probably already exists in a module, I haven't checked):

# changebase($number, $from[, $to]); print changebase(255, 10, 16); # FF print changebase(0xFF, 16, 10); # 255 print changebase('FF', 16); # assume change to base 10 when no 'to' i +s supplied # 255 print changebase('FF', 16, 2); # 11111111
An OO module could even be more transparent:
use Base; $hex = new Base 16 => 'FF'; print $hex; # Stringify # FF print $hex->binary; # Return value only ($hex->binary is an alias f +or $hex->base 2) # 11111111 print $hex; # FF print $hex->to_binary; # Change value # 11111111 print $hex; # 11111111
After all that, my main point is that I absolutely agree that those two functions are completely non-intuitive.
"Get real! This is a discussion group, not a helpdesk. You post something, we discuss its implications. If the discussion happens to answer a question you've asked, that's incidental." -- nobull@mail.com in clpm

In reply to Re: A philosophical pondering concerning hexes by BigLug
in thread A philosophical pondering concerning hexes by Llew_Llaw_Gyffes

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-18 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found