I am having trouble understanding exactly what kind of data that you really have. This matters. Do you have real binary? Or are you using some textual string representation of binary data?

AnomalousMonk immediately converted your string into "real binary", which is not unreasonable as you can't post a binary file here, you'd have to uuencode it or whatever. It could be that you are working from a spec and don't have a real binary file to work from. Below shows to write one to disk for testing.

#we are packing nibbles (4 bits) at a time # H is big-endian order # h is little-endian order (swaps adjacent nibbles) my $raw = pack 'H*', '8447000c0000110a0350'; #decode back to input string... print unpack ('H*', $raw), "\n"; # prints: 8447000c0000110a0350 print unpack ('h*', $raw), "\n"; # prints: 487400c0000011a03005 #need to set binmode when writing raw binary data... open (BIN, '>', 'test.bin') || die "can't open test.bin"; binmode(BIN); print BIN $raw; close BIN;
Yes, there are a number of ways to write the code that gets the length:
# v1 is a little-endian 16 bit (short) so needs 2 bytes # if substr didn't limit to 2 bytes substr($raw,1) works also # unpack will use first 2 bytes. my ($len) = unpack 'v1',substr($raw,1,2);#substr deals in bytes not ni +bbles. print $len,"\n"; #prints 71 print unpack ('v', substr($raw,1)),"\n"; #prints 71 print unpack ('x v', $raw ),"\n"; #prints 71
so questions:
1. Do you really have real binary or are you just working from this "nibblelist" format in ASCII?
2. It would be helpful if you had a little chart explaining or the C record declaration or equivalent in whatever format you can:
record: unsigned char record_type 8 bits unsigned short length 16 bits little endian ... some data type and description of these sections...
After see they single byte of type and the 2 bytes (little endian) of length, then there are 3 different types of data sections that can follow depending upon the type, please explain that a bit more - not sure that I really get it.

I would suggest reading more about substr. I think you probably want to leave off REPLACEMENT parm. The $str = substr eXPR, OFFSET, LENGTH, REPLACEMENT

If you really want to read binary, read more about read as you will have to set binmode and also pay attention to the return value of read which is the actual number of bytes read. All of this is different if you just have an ASCII representation of binary instead of "real binary".


In reply to Re^3: How to sort record using pack function ? by Marshall
in thread How to sort record using pack function ? by bh_perl

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.