-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowNumberInBases
executable file
·394 lines (311 loc) · 10.2 KB
/
showNumberInBases
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#!/usr/bin/env perl -w
#
# showNumberInBases: Convert numbers to various bases and forms.
# ~2007-11: Written by Steven J. DeRose.
#
use strict;
use Getopt::Long;
use Encode;
use charnames ':full';
use Date::Format;
use sjdUtils;
our %metadata = (
"title" => "showNumberInBases",
"description" => "Convert numbers to various bases and forms.",
"rightsHolder" => "Steven J. DeRose",
"creator" => "http://viaf.org/viaf/50334488",
"type" => "http://purl.org/dc/dcmitype/Software",
"language" => "Perl 5.18",
"created" => "~2007-11",
"modified" => "2021-05-07",
"publisher" => "http://github.com/sderose",
"license" => "https://creativecommons.org/licenses/by-sa/3.0/"
);
our $VERSION_DATE = $metadata{"modified"};
=pod
=head1 Usage
showNumberInBases [options] [numbers]
Display the input number(s) or times in several bases (or other forms)
Specifically:
=over
=item * as a character, if in ASCII range
=item * as a Unicode UTF-8 byte sequence, if above the ASCII range
=item * as RGB color codes and the nearest X color name,
if starts with "#" (backslash the "#" if needed).
The "#" may be followed by
3, 6, 9, or 12 hexadecimal digits I<without> intervening punctuation
(for example, C<#FF0000>), or (soon)
3 groups of decimal digits I<with> intervening commas or other punctuation
(for example, C<#255,0,00000>).
=item * as a time (hh:mm:ss) if you specify -t.
=back
I<num> can be decimal, hex (0xFFF), octal (0777), or binary (0b1010);
and can have a suffix of K, M, G, T, or P for powers of 1024 (like ls -h),
or k, m, g, t, or p for powers of 1000.
=head1 Options
=over
=item * B<-l> or B<--lines>
Put each base on a separate line.
=item * B<-k> or B<--human>
Print big numbers in units of K, M, G, T, P, E, Z, Y.
=item * B<-t> or B<--time>
Format a number of seconds as an H:M:S duration, and a Unix epoch time.
=item * B<--tf> or B<--timeFormat>
Change the I<-t> epoch time format from the usual C form, to this.
See L<https://metacpan.org/pod/Date::Format> for information on how
to specify a format. ISO 8601 is "%Y-%m-%dT%H:%M%S".
=back
=head1 Known bugs and limitations
Knows nothing about any color models but RGB.
Time units larger than hours are not calculated.
Doesn't have output in comma-grouped decimal, or in units of 1024**n.
=head1 Related commands
C<chr>, C<ord> -- convert between characters and code point numbers.
C<showrgb> -- displays list of available X colors.
C<findColorName> -- used to find the nearest named X colors.
C<ord>, C<CharDisplay.py> -- much more info about character sets,
Unicode code points, etc.
C<sjdUtils.pm> -- Supplies h-number conversion.
=head1 History
~2007-11: Written by Steven J. DeRose.
2007-11-~~ sjd: Add -k, -t, -u, input multiplier letters.
2010-09-12 sjd: Cleanup.
2011-06-29 sjd: Eine kleine nichtKleenup.
2012-02-23 sjd: Add #rgb support.
2012-03-22 sjd: Handle multiple numbers. Support decimal colors #d,d,d
2021-05-07: New layout. Switch to hashes of units, and add Eleven Zeppelins, Yo.
Better 0b formatting. Add --line. Better Unicode reporting.
With -t, give date assuming it's a Unix time-stamp. Add --timeFormat.
=head1 To do:
Make short colors round, not just shift?
Handle UTF-8 input (nah, ord and CharDisplay do it)
Break long numbers into groups of digits via sjdUtils::lpadc().
Support negative numbers.
=head1 Rights
Copyright 2007 by Steven J. DeRose. This work is licensed under a
Creative Commons Attribution-Share Alike 3.0 Unported License.
For further information on this license, see
L<https://creativecommons.org/licenses/by-sa/3.0>.
For the most recent version, see L<http://www.derose.net/steve/utilities> or
L<https://github.com/sderose>.
=cut
###############################################################################
# Options
#
my $humanUnits = 0;
my $lines = 0;
my $quiet = 0;
my $time = 0;
my $timeFormat = "%C";
my $verbose = 0;
Getopt::Long::Configure ("ignore_case");
my $result = GetOptions(
"h|help|?" => sub { system "perldoc $0"; exit; },
"k|human!" => \$humanUnits,
"l|lines!" => \$lines,
"q|quiet!" => \$quiet,
"t|time!" => \$time,
"tf|timeFormat=s" => \$timeFormat,
"v|verbose+" => \$verbose,
"version" => sub {
die "Version of $VERSION_DATE, by Steven J. DeRose.\n";
}
);
($result) || die "Bad options.\n";
($ARGV[0]) ||
die "No argument specified.\n";
my $phrase = "Karl Marx Gave The Proletariat Eleven Zeppelins Yo";
my @prefixOrder = $phrase =~ m/([A-Z])/g;
my %by1000 = (
'k' => 1000**1,
'm' => 1000**2,
'g' => 1000**3,
't' => 1000**4,
'p' => 1000**5,
'e' => 1000**6,
'z' => 1000**7,
'y' => 1000**8,
);
my %by1024 = (
'K' => 1024**1,
'M' => 1024**2,
'G' => 1024**3,
'T' => 1024**4,
'P' => 1024**5,
'E' => 1024**6,
'Z' => 1024**7,
'Y' => 1024**8,
);
my $k = 1000**1;
my $m = 1000**2;
my $g = 1000**3;
my $t = 1000**4;
my $p = 1000**5;
my $e = 1000**6;
my $z = 1000**7;
my $y = 1000**8;
my $K = 1024*1;
my $M = 1024**2;
my $G = 1024**3;
my $T = 1024**4;
my $P = 1024**5;
my $E = 1024**6;
my $Z = 1024**7;
my $Y = 1024**8;
my $tot;
while (defined ($tot=shift)) {
($verbose) && print "Got '$tot'\n";
if ($tot =~ m/^#[0-9a-f]{3,}/i) {
decodeColor($tot);
next;
}
$tot = int($tot);
$tot = handleScaleSuffix($tot);
$tot = oct($tot) if ($tot =~ m/^0/);
if ($time) {
print timeCode($tot) . "\n";
print time2str($timeFormat, $tot) . "\n";
next;
}
my $buf = sprintf(" oct %12o, dec %12d, hex %12x, bin %s",
$tot, $tot, $tot, formatBinary($tot));
if ($lines) { $buf =~ s/, /\n /g; }
print "$buf\n";
if ($humanUnits && $tot>1023) {
$buf = "\n";
for my $unit (@prefixOrder) {
#warn sprintf("unit %s, factor %d\n", $unit, $by1024{$unit});
my $val = $tot/$by1024{$unit};
$buf .= sprintf(" %12.2f%s\n", $val, $unit);
($val < 0.01) && last;
}
print $buf;
}
if (!$quiet && $tot < 0xFFFE) {
print " Printable character: '" . chr($tot) . "'\n";
print " If that's a Unicode code point:\n";
printf(" UTF-8: %s\n", sjdUtils::getUTF8($tot));
printf(" literal: '%s'\n", chr($tot));
my $uname = charnames::viacode($tot);
if ($uname) { print " name: $uname\n"; }
}
} # shift
exit;
###############################################################################
# Make prettier binary display
#
sub formatBinary {
my ($n) = @_;
my $buf = '';
while ($n > 0) {
my $byte = $n & 0x00FF;
$buf = sprintf("%08b ", $byte) . $buf;
$n = $n >> 8;
}
return $buf;
}
# See if user typed in a number with a suffix letter, and figure it in.
#
sub handleScaleSuffix {
my ($tot) = @_;
$tot =~ m/\d+([KMGTP])$/i;
if (defined $1) {
my $unit = $1;
if ($unit eq "k") { $tot *= $k; }
elsif ($unit eq "m") { $tot *= $m; }
elsif ($unit eq "g") { $tot *= $g; }
elsif ($unit eq "t") { $tot *= $t; }
elsif ($unit eq "p") { $tot *= $p; }
elsif ($unit eq "K") { $tot *= $K; }
elsif ($unit eq "M") { $tot *= $M; }
elsif ($unit eq "G") { $tot *= $G; }
elsif ($unit eq "T") { $tot *= $T; }
elsif ($unit eq "P") { $tot *= $P; }
}
return($tot);
}
###############################################################################
#
sub decodeColor {
my ($raw) = @_;
$raw =~ s/^#//;
my $len = length($raw);
my $digitsPer = int($len/3);
my $r = my $g = my $b = 0;
if ($raw =~ m/^(\d+),(\d+),(\d+)\s*$/) { # Decimal
$r = sprintf("%02x",$1);
$g = sprintf("%02x",$2);
$b = sprintf("%02x",$3);
if ($r>255 || $g>255 || $b>255) {
warn "Value out of range\n";
}
$digitsPer = 2; # as if hex
}
elsif ($len == 3) { # Hex RGB
$raw =~ m/^(.)(.)(.)/ || warn "not 3? '$raw'\n";
($r, $g, $b) = ($1, $2, $3);
}
elsif ($len == 6) { # RRGGBB
$raw =~ m/^(..)(..)(..)/ || warn "not 6? '$raw'\n";
($r, $g, $b) = ($1, $2, $3);
}
elsif ($len == 9) { # RRRGGGBBB
$raw =~ m/^(...)(...)(...)/ || warn "not 9? '$raw'\n";
($r, $g, $b) = ($1, $2, $3);
}
elsif ($len == 12) { # RRRRGGGGBBBB
$raw =~ m/^(....)(....)(....)/ || warn "not 12? '$raw'\n";
($r, $g, $b) = ($1, $2, $3);
}
else {
warn "Can't decode color string, '$raw' not a known length.\n";
return;
}
print " Component d[255] x[FF] x[FFFF] %[100]\n";
my $r2 = showColor("Red", $r, $digitsPer);
my $g2 = showColor("Green", $g, $digitsPer);
my $b2 = showColor("Blue", $b, $digitsPer);
print `findColorName $r2 $g2 $b2` . "\n";
}
sub showColor {
my ($name, $hex, $digits) = @_;
my $d = my $x2 = my $x4 = my $pct = 0;
if ($digits == 1) {
$d = hex("0x0$hex")*16;
$x2 = $hex . "0";
$x4 = $hex . "000";
$pct = $d * 100.0 / 255.0;
}
elsif ($digits == 2) {
$d = hex("0x0$hex");
$x2 = $hex;
$x4 = $hex . "00";
$pct = $d * 100.0 / 255.0;
}
elsif ($digits == 3) {
$d = int(hex("0x0$hex")/16);
$x2 = substr($hex,0,2);
$x4 = $hex . "0";
$pct = $d * 100.0 / 255.0;
}
elsif ($digits == 4) {
$d = int(hex("0x0$hex")/256);
$x2 = substr($hex,0,2);
$x4 = $hex;
$pct = $d * 100.0 / 255.0;
}
printf(" %-10s %03d %2s %4s %6.2f\n",
$name, $d, $x2, $x4, $pct);
return($x2);
}
###############################################################################
# timeCode: Change elapsed seconds into hh:mm:ss form.
# Params: 0: Time as a number of seconds.
#
sub timeCode {
my $etime = $_[0];
my $s = $etime % 60;
my $m = int($etime/60) % 60;
my $h = int($etime/3660);
return(sprintf("%02d:%02d:%02d", $h, $m, $s));
}