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; } }

In reply to Generating characters (0 to 255) by parv

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



  • 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:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.