Saturday, July 4, 2009

adventures in ignorance: hex vs oct

At one time I must have known this, but, like many parts of Perl I don't use on a common basis, it must have fallen out of my head. The hex function does exactly what I would expect it to do; that is it turns a string of hexadecimal digits into a Perl number. It can also handle strings that start with "0x". However, the oct function does significantly more than I would expect it to. In addition to converting strings of octal digits to Perl numbers, it can convert hexadecimal numbers (if they start with "0x") and binary numbers (if they start with "0b"). The reason for this is obvious: hex can't determine if "0b10" is binary for 2 or hexadecimal for 2_832, but oct can. There is no common convert function for the same reason. I do still find it odd that hex throws a warning and returns zero when confronted with leading spaces, but has no problem with trailing spaces.

#!/usr/bin/perl

use perl5i;

my @strings = ("10 ", " 10", "0b10", "010", "0x10");

say "testing hex:";
for my $string (@strings) {
say "\thex '$string' is '", hex $string, "'";
}

say "testing oct:";
for my $string (@strings) {
say "\toct '$string' is '", oct $string, "'";
}

2 comments:

  1. Yay! Support for binary strings was my favorite new feature in Perl 5.6. Not just with oct, but specifying numbers with 0b and using "%b" in sprintf and friends. I don't know why they didn't just add a "bin" function (analogous to oct and hex), but I wasn't going to complain that they did it with oct instead. Cray C had this years ago and it spoiled me for other C compilers. I wish they would add it to ANSI C.

    ReplyDelete
  2. In someways I happy that they didn't add 0b to ANSI C. If they had I wouldn't have learned hexadecimal as well as I have. The direct mapping from a hex digit to four binary digits is just too useful. Really, which would you rather 0xa2 or 0b10100010? That said, Perl's ability to understand numbers with underscores in them makes the binary a little more palatable: 0x1010_0010.

    ReplyDelete

Some limited HTML markup is allowed by blogger: strong, b, i, and a. You may also use em, but I have repurposed it through the magic of CSS to be behave very much like <tt><code></code></tt>.