256 |
warn "## checksum ",dump( $bytes, $xor, $checksum ) if $debug; |
warn "## checksum ",dump( $bytes, $xor, $checksum ) if $debug; |
257 |
|
|
258 |
my $len = ord(substr($bytes,2,1)); |
my $len = ord(substr($bytes,2,1)); |
259 |
my $len_real = length($bytes); |
my $len_real = length($bytes) - 1; |
260 |
print "length wrong: $len_real != $len\n" if $len_real != $len; |
|
261 |
|
if ( $len_real != $len ) { |
262 |
|
print "length wrong: $len_real != $len\n"; |
263 |
|
$bytes = substr($bytes,0,2) . chr($len_real) . substr($bytes,4); |
264 |
|
} |
265 |
|
|
266 |
if ( defined $checksum && $xor ne $checksum ) { |
if ( defined $checksum && $xor ne $checksum ) { |
267 |
print "checksum doesn't match: ", as_hex($xor), " != ", as_hex($checksum), " data: ", as_hex($bytes), "\n"; |
print "checksum doesn't match: ", as_hex($xor), " != ", as_hex($checksum), " data: ", as_hex($bytes), "\n"; |
301 |
sub str2bytes { |
sub str2bytes { |
302 |
my $str = shift || confess "no str?"; |
my $str = shift || confess "no str?"; |
303 |
my $b = $str; |
my $b = $str; |
304 |
$b =~ s/\s+(\S\S)(\S\S)+\s*/ $1 $2/; # fix checksum |
$b =~ s/\s+//g; |
305 |
$b =~ s/\s+$//; |
$b =~ s/(..)/\\x$1/g; |
306 |
$b =~ s/\s+/\\x/g; |
$b = "\"$b\""; |
|
$b = '"\x' . $b . '"'; |
|
307 |
my $bytes = eval $b; |
my $bytes = eval $b; |
308 |
die $@ if $@; |
die $@ if $@; |
309 |
warn "## str2bytes( $str ) => $b => ",as_hex($bytes) if $debug; |
warn "## str2bytes( $str ) => $b => ",as_hex($bytes) if $debug; |