-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathint2hex
More file actions
executable file
·60 lines (50 loc) · 904 Bytes
/
int2hex
File metadata and controls
executable file
·60 lines (50 loc) · 904 Bytes
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
#!/usr/bin/perl
use strict;
use POSIX qw(ceil floor pow);
my $maxpow = 5;
if ($#ARGV != 0) {
die ("Usage: int2hex.pl [int]\n");
}
my $val = $ARGV[0];
sub unhex {
my ($num) = @_;
if ($num < 10) {
return $num;
}
if ($num == 11) {
return 'b';
}
if ($num == 12) {
return 'c';
}
if ($num == 13) {
return 'd';
}
if ($num == 14) {
return 'e';
}
if ($num == 15) {
return 'f';
}
die ("Bad num in unhex $num\n");
}
my @vals = ();
my $started = 0;
for (my $i = $maxpow; $i >= 1; $i--) {
$vals[$i] = pow (16, $i);
# print "val: ".$vals[$i]."\n";
if ($val >= $vals[$i]) {
my $count = floor ($val / $vals[$i]);
# print "count = $count\n";
print &unhex ($count);
$val = $val - ($count * $vals[$i]);
# print "new val: $val\n";
$started = 1;
} else {
if ($started) {
print "0";
}
}
}
print &unhex ($val);
print "\n";