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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.