The critical point to remember is that
    $rgb[16]=pack("S", 7360);  # Overwrite 3689 with 7360
does not assign 7360 to  $rgb[16] but instead assigns a two-byte string that is the S-packed representation of 7360. After the pair of assignments in your OPed code,  @rgb contains a Hellish mix of numbers and strings. Don't do that. Instead, just assign numbers to the array elements and re-pack:

c:\@Work\Perl>perl -wMstrict -MData::Dump -le "my @ra = (1, 3689, 2, 2462, 3); dd \@ra; ;; my $pS1 = pack 'S*', @ra; dd $pS1; print 'length $pS1 == ', length $pS1; ;; my @rb = unpack 'S*', $pS1; dd \@rb; ;; $rb[1] = 7360; $rb[3] = 4912; my $pS2 = pack 'S*', @rb; dd $pS2; print 'length $pS2 == ', length $pS2; ;; my @rc = unpack 'S*', $pS2; dd \@rc; " [1, 3689, 2, 2462, 3] "\1\0i\16\2\0\x9E\t\3\0" length $pS1 == 10 [1, 3689, 2, 2462, 3] "\1\0\xC0\34\2\x000\23\3\0" length $pS2 == 10 [1, 7360, 2, 4912, 3]


Give a man a fish:  <%-{-{-{-<


In reply to Re: Write 2 uint16_t numbers to file in Perl by AnomalousMonk
in thread Write 2 uint16_t numbers to file in Perl by BrianP

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.