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 | |
by tachyon (Chancellor) on Mar 28, 2003 at 00:23 UTC | |
by Aristotle (Chancellor) on Mar 30, 2003 at 21:27 UTC | |
by parv (Parson) on Mar 27, 2003 at 21:12 UTC |