filesynced.t: Skip tests if `~/bin/synced.sql` doesn't exist
[sunny256-utils.git] / tohex
blobc5dd164cd7f221cbb6bd2df15f40c74e25d738f0
1 #!/usr/bin/env perl
3 #=======================================================================
4 # tohex
5 # File ID: 3ced9aa2-f744-11dd-9353-000475e441b9
7 # Convert files to hex
9 # Character set: UTF-8
10 # ©opyleft 2008– Ø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 #=======================================================================
15 use strict;
16 use warnings;
17 use Getopt::Long;
19 local $| = 1;
21 our %Opt = (
23 'decimal' => 0,
24 'help' => 0,
25 'quiet' => 0,
26 'unicode' => 0,
27 'verbose' => 0,
28 'version' => 0,
32 our $progname = $0;
33 $progname =~ s/^.*\/(.*?)$/$1/;
34 our $VERSION = '0.1.0';
36 Getopt::Long::Configure('bundling');
37 GetOptions(
39 'decimal|d' => \$Opt{'decimal'},
40 'help|h' => \$Opt{'help'},
41 'quiet|q+' => \$Opt{'quiet'},
42 'unicode|u' => \$Opt{'unicode'},
43 'verbose|v+' => \$Opt{'verbose'},
44 'version' => \$Opt{'version'},
46 ) || die("$progname: Option error. Use -h for help.\n");
48 $Opt{'verbose'} -= $Opt{'quiet'};
49 $Opt{'help'} && usage(0);
50 if ($Opt{'version'}) {
51 print_version();
52 exit(0);
55 my $print_str = $Opt{'decimal'} ? "%u" : "%02x";
56 my $spc = '';
57 my $count = 0;
59 exit(main());
61 sub main {
62 # {{{
63 my $Retval = 0;
65 if (scalar(@ARGV)) {
66 warn("$progname: $ARGV[0]: Unknown command line argument\n");
67 exit(1);
70 if ($Opt{'unicode'}) {
71 binmode(STDIN, ':utf8');
74 while (<STDIN>) {
75 s/(.)/print_byte(ord($1))/gse;
76 print;
78 print "\n";
80 return $Retval;
81 # }}}
82 } # main()
84 sub print_byte {
85 # {{{
86 my $val = shift;
87 if ($count > 15) {
88 print("\n");
89 $count = 0;
90 $spc = '';
92 printf("$spc$print_str", $val);
93 $count++;
94 $spc = ' ';
95 return('');
96 # }}}
97 } # print_byte()
99 sub print_version {
100 # Print program version {{{
101 print("$progname $VERSION\n");
102 return;
103 # }}}
104 } # print_version()
106 sub usage {
107 # Send the help message to stdout {{{
108 my $Retval = shift;
110 if ($Opt{'verbose'}) {
111 print("\n");
112 print_version();
114 print(<<"END");
116 Usage: $progname [options]
118 Options:
120 -d, --decimal
121 Use decimal (10 base) output instead of hex.
122 -h, --help
123 Show this help.
124 -q, --quiet
125 Be more quiet. Can be repeated to increase silence.
126 -u, --unicode
127 Output Unicode values instead of bytes when reading UTF-8. For
128 example, when reading the character U+2620, output "2620" instead of
129 "e2 98 a0" when this option is used. Likewise, when combined with
130 --decimal, output "9760" instead of "226 152 160".
131 -v, --verbose
132 Increase level of verbosity. Can be repeated.
133 --version
134 Print version information.
137 exit($Retval);
138 # }}}
139 } # usage()
141 sub msg {
142 # Print a status message to stderr based on verbosity level {{{
143 my ($verbose_level, $Txt) = @_;
145 if ($Opt{'verbose'} >= $verbose_level) {
146 print(STDERR "$progname: $Txt\n");
148 return;
149 # }}}
150 } # msg()
152 __END__
154 # This program is free software; you can redistribute it and/or modify
155 # it under the terms of the GNU General Public License as published by
156 # the Free Software Foundation; either version 2 of the License, or (at
157 # your option) any later version.
159 # This program is distributed in the hope that it will be useful, but
160 # WITHOUT ANY WARRANTY; without even the implied warranty of
161 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
162 # See the GNU General Public License for more details.
164 # You should have received a copy of the GNU General Public License
165 # along with this program.
166 # If not, see L<http://www.gnu.org/licenses/>.
168 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :