Hello rem45acp,
Here is one solution using manual bit-twiddling:
#! perl use strict; use warnings; my $orig = 0b10100111_00010000; my $text = error_state('E') + (mode_and_status(7, 'B', 1) << 8); die "$orig != $text: $!" unless $orig == $text; my $msg = pack ('n', $text); my $decode = unpack('n', $msg); print "text: >$text<\n"; print "msg: >$msg<\n"; print "decode: >$decode<\n"; die "$decode != $text: $!" unless $decode == $text; sub error_state { my ($error_type) = @_; my $shift_count = ord($error_type) - ord('A'); return 1 << $shift_count; } sub mode_and_status { my ($mode, $status, $additional_status) = @_; my $status_shift = ord($status) - ord('A'); return $mode + (1 << $status_shift + 4) + ($additional_status << 7 +); }
(Error handling has been left as the proverbial exercise for the reader!)
But prefer AnomalousMonk’s approach using pack with a 'b' or 'B' specifier, as this will no doubt lead to a more elegant solution.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re: Pack()-ing a Message
by Athanasius
in thread Pack()-ing a Message
by rem45acp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |