Do you still recommend unpack as opposed to ord.

The answer is a qualified "yes". I'll get to the qualification in bit.

Ostensibly, once you have stripped the header from your binary data, extracting your 16-bit, big-endian values from string would be a simple as

my @ADsamples = unpack 'n*', $binaryData;

Which will 'do the right thing' with big-endian 16 bit data regardless of the architecture that it runs on.

However, there is a caveat as I mention a the top. The 'n' upack format specifier is for unsigned 16-bit values. There is no equivalent for big-endian, signed 16-bit data.

The upshot of this is that if your original data has the msb of the original sample is set, once this is sign extended, the resultant will be a negative value. Treating this as an unsigned value will result in large positive values!

This may not be a problem as you maybe oring out the bits that you need and ignoring the machinations of the sign extension. Otherwise, getting the negative values back is fairly trivial.

@signed = map{ $_ > 32767 ? $_ - 65536 : $_ } @unsigned;

So long as your aware of it, no problem.

That does bring up one other matter though,related to your use of ord. Depending on how you're breaking out the values from your sting, and which version of Perl you are using, ord has a trap waiting for the unwary using it to manipulate non-character data, namely utf.

In recent version of Perl (>5.6.1 I think but I'm not certain), ord can return values >255. Additionally, depending on what function you use and what regex (if any) you use, attempting to break a scalar into chars will not necessarially render bytes. split/./, $binaryData; is going to attempt to treat your data as variable-length chars if it discovers anything in the data that looks like a ucs char. This also holds true for  my @bytes = /./g; for instance unless you take precations to prevent it.

You may already be aware of this and taking the appropriate steps, but as I fell into the trap myself very recently, I thought it worth mentioning.


Nah! You're thinking of Simon Templar, originally played (on UKTV) by Roger Moore and later by Ian Ogilvy

In reply to Re: Re: perl in DOS woes by BrowserUk
in thread perl in DOS woes by manzico

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.