forked from samyk/samytools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhex2bin
executable file
·61 lines (51 loc) · 1.23 KB
/
hex2bin
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
#!/usr/bin/perl
# convert intel hex file to binary
# useful when wanting to drop a .hex into a disassembler
# -samy kamkar
use strict;
my $H = "[A-Fa-f0-9]";
while (<>)
{
chomp;
s/\r$//;
if (/^:(..)(....)(..)(.*)(..)$/)
{
my ($count, $addy, $record, $data, $ck) = ($1, $2, $3, $4, $5);
# let's calculate our checksum
my $calcck = 0;
$calcck += $_ for map { hex } split /(..)/, $count . $addy . $record . $data;
$calcck = 0xFF & (0x01 + ~$calcck);
$calcck = uc(unpack("H2", chr($calcck)));
$count = ord(pack("H2", $count));
$addy = unpack("S", (pack("H4", $addy)));
$record = ord(pack("H2", $record));
print STDERR "cnt=$count addy=$addy rec=$record data=$data ck=$ck (calcck=$calcck) $_\n";
# validate checksum
if ($ck ne $calcck)
{
print STDERR "> !!! Bad checksum! $calcck - $ck\n";
}
# validate length
if ($count * 2 != length($data))
{
print STDERR "> !!! Incorrect length! $count\n";
}
# data
if ($record == 0)
{
print pack("H*", $data);
}
elsif ($record == 1)
{
print STDERR "> End of file\n";
}
else
{
print STDERR "> Record $record: " . pack("H*", $data) . "\n";
}
}
else
{
die "Unknown2: $_\n";
}
}