#!/usr/bin/perl -w use strict; my $buf; { local $/ = undef; $buf = <>; } my @buf = unpack("(C)*", $buf); my $state = 'record'; my $count_bytes = 0; my $record_len = 0; sub getcount { $record_len += 256**$count_bytes * $_; $count_bytes++; if ($count_bytes == 2) { $count_bytes = 0; $state = 'data'; print " "; } } my $fsm = { record => sub { $state = 'count'; print " "; $record_len = 0; }, count => \&getcount, data => sub { $record_len--; if ($record_len == 1) { $state = 'checksum'; print " "; } }, checksum => sub { $state = 'record'; print "\n"; }, }; foreach (@buf) { printf "%02X", $_; $fsm->{$state}->(); }