Hi monks,

I'm using Perl to parse the byte stream output of a hardware.
Depending on the HW configuration I get a stream of data consisting
of 40, 48, 56 or 64 bits litle-endian.
In principle the lower 5,6,7 or the complete 8 byte of a 64bit litle-endian integer.
I was trying to convert this data using unpack but whatever I tried using 'x' or '@'
I did not succeed in (p)adding the missing 0 bytes before converting to a 64bit integer.
(I'm using a Perl with support for 64bit integers)

My current solution looks like: (using bitstrings..)
my $bytes_per_value = 5; # to simulate the byte stream using 40bit = 5 * 8bit; my $value = 0xf_dead_beef_4; my $bin_value = substr (pack ('Q', $value), 0, $bytes_per_value); my $buffer = $bin_value x 4 my $nbytes = length ($buffer); my $fmt = sprintf "(b%d)*", $bytes_per_value << 3; my @stream_data = unpack ($fmt, substr ($buffer, 0, $nbytes)); my @values = map { oct '0b'.reverse ($_)} @stream_data; foreach my $v (@values) { printf "0x%x\n", $v; }

My question:
Is there a way to unpack this stream directly into an array of QWords (64bit)
using some form of unpack for 5,6 and 7 byte data. (The 8 byte case is obviously easy :-) )

Means: Can I specify via the format string to convert 5,6 or 7 bytes + 3,2 or 1 padding Zero bytes
to a 64bit integer?
Or can unpack only work on "existing" data bytes.

Thanks for any hints!

Axel

In reply to Can unpack add zero bytes before converting? by mossi2000

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.