in reply to Problems Getting the Template Correct when Using unpack()
Your specification of C33 returns a list of 33 unsigned numbers. You need an array to hold them. Another option is to use the array, but fill it with a list of characters ( use (a)33 ).
Ahhh..! Tha's the biggest clue, methinks... and something I was (half) mindful of - treating the list of bytes as a string when it really should be an array.
So, the way I'm looking to proceed now is along the following lines (code follows):-
use strict; use warnings; use Encode; my @data = ( 0x2E, 0x75, 0x0A, 0x04, 0x00, 0x00, 0x00, 0x10, 0x57, 0x5 +6, 0x32, 0x20, 0x20, 0x20, 0x80, 0x49, 0x3D, 0x34, 0x32, 0x3 +7, 0x20, 0x4C, 0x61, 0x6D, 0x70, 0x20, 0x64, 0x69, 0x6D, 0x2 +0, 0x73, 0x74, 0x61, 0x74, 0x65, 0x3D, 0x32, 0x20, 0x5B, 0x5 +3, 0x43, 0x41, 0x54, 0x53, 0x3D, 0x30, 0x5D, 0x2E ); my $inbuf = join('', map { chr($_) } @data); # Input buffer printf(" \$inbuf: >%s<\n", $inbuf); # ---- # Unpack event data:- # [event length] # [year][month][day][hour][minute][seconds] # [record type][source...] # [event data...] # [event length] # my ($H_event_length, $year, $mon, $day, $hour, $min, $sec, $rec_type_idx, $source_name, @xdata) = unpack("C8a6C*", $inbuf); (my $T_event_length) = splice(@xdata, $#xdata); # Strip & save t +he trailing event length # ... We can test for $H_event_length != $T_event_length => ERROR my $data_length = @xdata; # We ACTUALLY have this amount of data + my $calc_data_length = $H_event_length - (6 + 1 + 6); # ... We can test for $data_length != $calc_data_length => ERROR # ---- my $date_str = sprintf("%s/%s/%s", $day, $mon, $year+1900); my $time_str = sprintf("%02d:%02d:%02d", $hour, $min, $sec); $rec_type_idx &= 0x1F; # Only use b0-b6 printf(" \$H_event_length: %02Xx = %d\n", $H_event_length, $H_event_ +length); printf(" \$T_event_length: %02Xx = %d\n", $T_event_length, $T_event_ +length); printf(" \$data_length: %s\n", $data_length); printf(" \$calc_data_length: %s\n", $calc_data_length); printf(" \$date_str: %s\n", $date_str); printf(" \$time_str: %s\n", $time_str); printf(" \$rec_type_idx: %02Xx = %d\n", $rec_type_idx, $rec_type_i +dx); printf(" \$source_name: !%s!\n", $source_name); printf(" \@data: "); my $i = 1; foreach my $d (@xdata) { printf("%02X ", $d); printf("\n%s", " "x20) unless ($i++ % 16); } printf("\n"); printf("%s", " "x19); $i = 1; foreach my $d (@xdata) { printf("%2c ", $d); printf("\n%s", " "x19) unless ($i++ % 16); } printf("\n");
Again, I'm very appreciative of everyone's help in helping me get around this annoyingly 'simple' problem... :)
|
|---|