rem45acp has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have to pack a complicated message into a 16-bit word that is defined like so:

Part 1: Error State (Least Significant Byte)
Part 2: Mode/Status/Additional Status

Part 1 Error State is defined like so:
Bit 0: Error type A (Least Significant Bit)
Bit 1: Error type B
Bit 2: Error type C
Bit 3: Error type D
Bit 4: Error type E
Bit 5: Error type F
Bit 6: Error type G
Bit 7: Error type H (Most significant Bit)

Part 2: Mode/Status Additional Status is defined like so:
Mode: The mode is contained in the least significant 4 bits of the most significant byte. Mode numbers go 0 - 11.
Status:
Bit 12: Status Type A
Bit 13: Status Type B
Bit 14: Status Type C
Additional Status:
Bit 15: Reset on/off

I can't seem to get the pack() command right. I want to pack:
Error type E, Mode 7, Status Type B, Additional Status ON

my $msg = pack("n", pack("C", 0, 0, 0, 0, 1, 0, 0, 0), #error state pack("C", 7, 0, 1, 0, 1) );

Perl doc, http://perldoc.perl.org/functions/pack.html, does not say about little/big endian when it comes to packing chars.

Replies are listed 'Best First'.
Re: Pack()-ing a Message
by AnomalousMonk (Archbishop) on Jun 09, 2014 at 03:26 UTC

    You don't want the  C c pack template specifiers, but rather the  B b specifiers. See Exotic Templates in perlpacktut (a very informative tut), the "Bit Strings" sub-section (the very first one).

Re: Pack()-ing a Message
by Athanasius (Archbishop) on Jun 09, 2014 at 04:15 UTC