marinersk has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Flipping the Sign Bit in pack()'ed Value
by choroba (Cardinal) on Jun 09, 2025 at 12:50 UTC | |
by marinersk (Priest) on Jun 09, 2025 at 13:30 UTC | |
|
Re: Flipping the Sign Bit in pack()'ed Value
by NERDVANA (Priest) on Jun 09, 2025 at 14:39 UTC | |
by ikegami (Patriarch) on Jun 09, 2025 at 15:55 UTC | |
by marinersk (Priest) on Jun 10, 2025 at 02:39 UTC | |
by marinersk (Priest) on Jun 10, 2025 at 02:31 UTC |