3 #=======================================================================
5 # File ID: 17c58aec-f743-11dd-a648-000475e441b9
7 # Converts a hexadecimal string to plain text.
10 # ©opyleft 2006– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of
12 # file for legal stuff.
13 #=======================================================================
34 $progname =~ s/^.*\/(.*?)$/$1/;
35 our $VERSION = '0.1.0';
37 Getopt
::Long
::Configure
('bundling');
40 'decimal|d' => \
$Opt{'decimal'},
41 'help|h' => \
$Opt{'help'},
42 'quiet|q+' => \
$Opt{'quiet'},
43 'unicode|u' => \
$Opt{'unicode'},
44 'verbose|v+' => \
$Opt{'verbose'},
45 'version' => \
$Opt{'version'},
46 'warnings|w' => \
$Opt{'warnings'},
48 ) || die("$progname: Option error. Use -h for help.\n");
50 $Opt{'verbose'} -= $Opt{'quiet'};
51 $Opt{'help'} && usage
(0);
52 if ($Opt{'version'}) {
63 if ($Opt{'unicode'}) {
64 binmode(STDOUT
, ':utf8');
67 if ($Opt{'decimal'}) {
69 s/(\d+)/print_decimal($1)/seig;
72 if ($Opt{'unicode'}) {
73 if (!$Opt{'warnings'}) {
75 # Not very elegant to duplicate code like this, but 'no
76 # warnings' only works in the local scope.
78 s/([\da-f]+)/print(chr(hex($1)))/seig;
82 s/([\da-f]+)/print(chr(hex($1)))/seig;
87 while (my $line = <>) {
88 $line =~ s/[^\da-f]//sig;
90 $buf =~ s/([\da-f][\da-f])/print(chr(hex($1))), ''/seig;
102 if ($val > 255 && !$Opt{'unicode'}) {
103 warn("$progname: Cannot print byte value $val in bytewise mode, use -u\n");
106 if (!$Opt{'warnings'}) {
118 # Print program version {{{
119 print("$progname $VERSION\n");
125 # Send the help message to stdout {{{
128 if ($Opt{'verbose'}) {
134 Converts a hexadecimal string to plain text.
136 Usage: $progname [options] [file [files [...]]]
141 Create bytes from decimal (base 10) numbers.
145 Be more quiet. Can be repeated to increase silence.
147 Use Unicode mode. If any values are higher than U+007F (127), output
148 the character as a UTF-8 sequence instead of bytewise output. I.e.
149 the character U+263A will be output as the bytes "e2 98 ba" instead
152 Increase level of verbosity. Can be repeated.
154 Print version information.
156 Enable Perl warnings about invalid UTF-8. Use this if you want to
157 generate safe UTF-8 or be warned about invalid code points in
166 # Print a status message to stderr based on verbosity level {{{
167 my ($verbose_level, $Txt) = @_;
169 if ($Opt{'verbose'} >= $verbose_level) {
170 print(STDERR
"$progname: $Txt\n");
178 # This program is free software; you can redistribute it and/or modify
179 # it under the terms of the GNU General Public License as published by
180 # the Free Software Foundation; either version 2 of the License, or (at
181 # your option) any later version.
183 # This program is distributed in the hope that it will be useful, but
184 # WITHOUT ANY WARRANTY; without even the implied warranty of
185 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
186 # See the GNU General Public License for more details.
188 # You should have received a copy of the GNU General Public License
189 # along with this program.
190 # If not, see L<http://www.gnu.org/licenses/>.
192 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :