Originally i tried to ask in the chatterbox, but i think that i may have mis-translated what i am trying to do. So without further ado:

I have an ogg/opus audio file that i am trying to read the header, which is 28 bytes or 0x1C in length. Here is the code i am using to read this header:

#!/usr/bin/perl use strict; use warnings; open my $ogg_file, '<', shift or die $!; binmode($ogg_file); read_ogg_header($ogg_file); sub read_ogg_header { my ( $file ) = @_; read($file, my $buf, 28 ); my ( $ogg_magic, $ogg_version, $flags, $granule_position, $serial_ +number, $sequence_number, $checksum, $total_segments, $segment_size ) + = unpack('C4 C1 C1 C8 C4 C4 C4 C1 C1', $buf); print $ogg_magic; ### Expected it to print 'OggS' }
and here is the 28 byte header:
0x4F6767530002000000000000000083CA3DC0000000009F59730A0113

The more i think about this the more i wonder if unpack C is not what i want to do anyway. are the single bytes already in unpack C format? if so, how can i just store that value into the variable without any conversion so any further printing or processing the actual raw values eg print $ogg_magic will print "OggS"

Or maybe i am just completely confused by pack and unpack. i can use read() and i can easily print those values or translate them into expected formats, but i think unpack C is messing me up somehow? or ... OR... maybe i am just completely confused. If i need to elaborate any more please just let me know.

P.S. i guess what i am wondering in the end is, how can i make pack or unpack assign the same values as read($file, my $buf, n);? i can work with whatever data read stores (im assuming binary?) but i cannot get the same values/data to store in those variables with unpack or pack. hope i am clear, if not just give me a swift kick in the hind quarters and tell me what other info you need. and as always any help is certainly appreciated :)


In reply to pack/unpack woes by james289o9

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.