Now and then i need to know hexadecimal/decimal value of a character. To deal w/ that i wrote following two functions, which print characters of values 0x00-0xff.

Update:In hex_dec_list(), current number is only specified once instead of thrice after i discover that i could specify index of parameter to use for formatting...

# before printf " %2x %3u: %c |" , ($_ , $_ , $_); # after printf ' %1$2x %1$3u: %1$c |' , $_;

One minor change was to use single quotes (instead of double) due to above change for printf() everywhere. Other was to make for loop to be a modifier for row separator creation in hex_table().

# print character table; column & row headings are compsed of hex num +bers sub hex_table { my @hex = (0..9 , 'a'..'f'); # number of characters each table cell takes my $cell_width = 4; # create row separator; adjust to your liking my $row_sep = "\n--"; $row_sep .= ($_ % $cell_width != 0) ? "-" : "." for 0 .. ( $cell_width * scalar(@hex) ); $row_sep .= "\n"; # space filler heading, common to column & row headings print '0x|'; # column heading printf ' %s |', $_ for @hex; foreach my $row (@hex) { print $row_sep; # row heading printf '%s |', $row; foreach my $col (@hex) { printf ' %c |', hex( $row . $col ); } } } # list characters 8 on each line; each character is preceded by its h +ex # & decimal number representation sub hex_dec_list { foreach (0..255) { # print hex, decimal numbers followed by character printf ' %1$2x %1$3u: %1$c |' , $_; # allow 8 sets per line print "\n" if $_ && ($_ + 1 ) % 8 == 0; } }

Replies are listed 'Best First'.
Re: Generating characters (0 to 255)
by cmilfo (Hermit) on Mar 27, 2003 at 20:23 UTC

    One Unix/Linux boxes, you can   man ascii    to print a table similar to those found in reference manuals.

    Below is something I use to generate an ascii table when I'm not not on a *nix machine and don't have a book. It's pretty much the same as your code, just in the table format.

    print "Decimal | Octal | Hex | Character\n"; print "--------+-------+-----+----------\n"; my @hs = (0 .. 9, 'A' .. 'F'); for my $r (@hs) { for my $c (@hs) { my $h = hex($r . $c); printf("%-8.8s\| %-6.6s\| %-4.4s\| %-s\n", sprintf("%u", $h), sprintf("%-3.3o", $h), sprintf("%-2.2X", $h), map({ s/\n/\^J/; $_ } sprintf("%c", $h))); } }

    Update: Removed padding on last column per parv's suggestion below. Great idea!

      Of course you are going to way too much effort to do this....

      print "Decimal | Octal | Hex | Character\n"; print "--------+-------+-----+----------\n"; printf "%-8s| %03o | %02X |%3s\n",($_)x3,map{$_=/\n/?'^J':/\r/?'^M' +:$_}chr for 1..255;

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        Capture regularities in code, irregularities in data.
        my %c = ( map(+(chr,sprintf '\x%02X',$_), 0 .. 31, 127 .. 160), "\a" => '\a', "\b" => '\b', "\e" => '\e', "\f" => '\f', "\n" => '\n', "\r" => '\r', "\t" => '\t', ); print "Decimal | Octal | Hex | Character\n"; print "--------+-------+-----+----------\n"; printf "%7d | %03o | %02X | %4s\n", ($_)x3, $c{+chr}||chr for 1..25 +5;

        Makeshifts last the longest.

      I must say output of your program is much easier on the the eyes.

      Since the character is the last item to be printed in your program, number of spaces it takes could be omitted and still get left justified output by using just printf("%-8.8s\| %-6.6s\| %-4.4s\| %-s\n", ...). Otherwise, empty (unoccupied) spaces are printed after each character (which could potentially interfere w/ copy-paste). What do you think?