awohld has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to fill an array with list of Hex numbers, 0x0 to 0xFF. When I print my array, it just shows decimal numbers in there. Is this just the way it works, or do I missing something to get Hex numbers in there?
#!/usr/bin/perl -w use strict; use Data::Dumper; my @hex_nums = ( 0x0 .. 0xFF ); print Dumper \@hex_nums;

Replies are listed 'Best First'.
Re: Hex Numbers and Range Operator
by Util (Priest) on Nov 18, 2006 at 03:41 UTC

    0xFF is just a different way of writing the decimal number 255; they are interchangeable in your Perl source code. Since decimal is the "native" number representation for humans, Perl outputs numbers in a decimal base, unless instructed otherwise.

    The printf or sprintf function will coerce the numbers into hex strings:

    my @hex_nums = map { sprintf '%02X', $_ } 0..255;

Silly Example (was Re: Hex Numbers and Range Operator)
by roboticus (Chancellor) on Nov 18, 2006 at 15:33 UTC
    awohld:

    Just to amplify a little on Util's response: A number is just a number and decimal, hex, octal, etc., are just different ways to represent it. To the computer it's just a bunch of bits.

    So, why have different representations? Some operations are easier to visualize and check in a different base. For example, I've done a boatload of assembly programming, so I would use hex frequently. One example: If you want to represent the ASCII code for the numbers 0..9 you could use decimal (48=='0', 49=='1', ... 57=='9'), octal (60=='0', 61=='1', ... 71=='9'), hex (30=='0', 31=='1', ... 39='9'), or any other base. But I would use hex because it's easy to remember "just add the digit I want to 30 to get the ASCII code for the digit".

    Another example: The same rule generally applies to the alphabet (0x41=='A', 0x42=='B', ...) which makes it fairly easy to convert from a letter to an ASCII code in decimal. And converting from UPPER CASE to lower case is easy, just OR the letter with 0x20 (0x61=='a', 0x62=='b', ...)

    If you're programming peripheral chips, such as UARTs or such, you'll find that many of them map specific functions to particular bits in a specific memory or I/O location. For example, the fictional 3141 DWIM chip performs various duties based on the value you write to 0x2718:

    bit 7: Take over the world bit 6: Turn on your porch light bit 5: Change your socks bit 4: Format your hard disk bit 3: Sell Z-80 computer on Ebay "No Reserve L@@K! RARE!" bit 2: Apply lipstick to pig bit 1: Make horse drink bit 0: Lead horse to water
    So if you write 57 decimal to that location, it's hard to tell what will happen just by looking at the number. Is your precious Z-80 computer safe? Will you take over the world? Will you change your socks? (Hey, it's not Tuesday!)

    But if you use binary, it's much simpler to tell. Let's see 57 converts to 0011 1001. Drat! Not only do I not get to control the world, but I've left my porch light off! Oh, no! My precious Z-80! Hmmmm.... I've lead the horse to water, but I forgot to make him drink!

    </foolish_mode>

    --roboticus

Re: Hex Numbers and Range Operator
by graff (Chancellor) on Nov 19, 2006 at 03:11 UTC
    I expect you've slapped your forehead by now, but in case you're still scratching it, check this out:
    #!/usr/bin/perl -w use strict; my @dec_nums = ( 0 .. 255 ); my @hex_nums = ( 0x00 .. 0xFF ); my @oct_nums = ( 000 .. 0377 ); my $msg = "OMG! Inequality among %d. vs. 0x%02X vs. \\%03o on test #%d +\n"; my $i; for $i ( 0 .. $#dec_nums ) { if ( $i != $dec_nums[$i] or $i != $hex_nums[$i] or $i != $oct_nums +[$i] ) { die sprintf( $msg, $dec_nums[$i], $hex_nums[$i], $oct_nums[$i] +, $i ); } } if ( @dec_nums != @hex_nums or @dec_nums != @oct_nums ) { die sprintf( $msg, scalar(@dec_nums), scalar(@hex_nums), scalar(@oct_nums), $i); } printf( "Whew! 0-%d. is the same as 0x00-0x%X and 000-0%o.\n", pop @dec_nums, pop @hex_nums, pop @oct_nums );