Okay, I am fairly certain I am missing something really, really fundamental here, and my brain just won't lock on to the pattern.

I humbly ask my fellow Monks to find the proper clue × four with which to soundly smack me upside the head:

#/user/bin/perl use strict; my $intval = 66; my $pckval = pack 'c', $intval; my $pckhex = "0x" . (unpack 'H16', "$pckval"); my $stoval = "$pckval" ^ 0x80; my $stohex = "0x" . (unpack 'H16', "$stoval");

Adding old-fashioned brute-force debugging to see what's going on:

print "\$intval = 66; # \$intval = [$intval] - Expecting [66]\n"; print "\$pckval = pack 'c', $intval; # \$pckval = [$pckval] - Expec +ting [B]\n"; print "\$pckhex = \"0x\" . (unpack 'H16', \"$pckval\"); # \$pckhex = +[$pckhex] - Expecting [0x42]\n"; print "\$stoval = \"$pckval\" ^ 0x80; # \$stoval = [$stoval] - Expe +cting [194]\n"; print "\$stohex = \"0x\" . (unpack 'H16', \"$stoval\"); # \$stohex = +[$stohex] - Expecting [0xc2]\n";

This produces results other than what I'm expecting, as shown:

W:\Steve\PerlMonks>perl tpm.pl $intval = 66; # $intval = [66] - Expecting [66] $pckval = pack 'c', 66; # $pckval = [B] - Expecting [B] $pckhex = "0x" . (unpack 'H16', "B"); # $pckhex = [0x42] - Expectin +g [0x42] $stoval = "B" ^ 0x80; # $stoval = [128] - Expecting [194] $stohex = "0x" . (unpack 'H16', "128"); # $stohex = [0x313238] - Ex +pecting [0xc2] W:\Steve\PerlMonks>

My dear fellow Monks, where have I gone astray?


In reply to Flipping the Sign Bit in pack()'ed Value by marinersk

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.