#!/usr/bin/env perl use strict; use warnings; use utf8; #=============================================================================== # Set to "1" for additional debugging print statements. #=============================================================================== my $debug= 1; #=============================================================================== # Standard input while loop #=============================================================================== my $line; # Retrieve a line while ($line = ) { # chomp line chomp $line; # Does line begin with ":"? if (($line !~ /^\:/) || ($line =~ /^\$/)) { print "INVALID LINE, SKIPPING : $line\n" if $debug; next } else { # convert line to hex (and strip off ":") my $hexrec = pack( 'H*', substr( $line, 1 ) ); # calculate checksum, if error, die. my $chksum = unpack( "%C*", $hexrec ); print "checksum = $chksum\n" if $debug; die unless unpack( "%8C*", $hexrec ) == 0; # # process the line. get bytecount, address, record type, and data # my( $bytecount, $address, $recordtype, $data ) = unpack( "C n C X4 C x3 /a", $hexrec ); # note: no reason to print data, since it results in special, unprintable chars printf ("bytecount = %d, address = %d, recordtype = %d\n", $bytecount, $address, $recordtype) if $debug; # # terminator line # if ($recordtype == 1) { print "Conversion complete\n"; exit; } # # real data line # elsif ($recordtype == 0) { my $binarycount = $bytecount*8; my $datlength = length($data); print "Data length (should match bytecount) = $datlength\n"; # convert to binary my $binaryval = unpack("B$binarycount", pack("H$bytecount", $data)); print "binary = $binaryval\n" if $debug; } } } # end of while __DATA__ :1004D0000A95E1F7E0E0F8E000E40BBF00E81791CF :0E04E0001D930A95E1F74CCE00E10883FDCE96 :00000001FF __END__