Greeting brethren/sisteren,
I have a problem that I'm trying to figure out, but I seem to be stumpped. First, the relevant code:
#!/usr/local/perl5.006/bin/perl -w use strict; my $var = shift; print "var: $var\n"; my $temp = sprintf "%lx", $var; print "temp: $temp\n"; my $packed = pack "H*", $temp; print "packed: $packed\n"; $temp = unpack "H*", $packed; print "temp: $temp\n"; my $unpacked = hex $temp; print "unpacked: $unpacked\n";
...and now the output:
>pack.pl 12 var: 12 temp: c packed: À temp: c0 unpacked: 192 >
I don't understand why when I unpack the variable $packed, it doesn't come back with what I packed it with. What I also find strange is that the behavior alternates depending on what range the input is in. Here's what I've found (x represents the input value):
16^0 <= x < 16^1 (# of hex digits is odd) bad 16^1 <= x < 16^2 (# of hex digits is even) good 16^2 <= x < 16^3 (# of hex digits is odd) bad 16^3 <= x < 16^4 (# of hex digits is even) good ...
Definately a pattern. The way I figure it, pack is packing this in to the appropriate number of bytes, and when it doesn't have enough to fill an entire byte, it 0 pads to the right. Don't know how to exploit that, however (other than a hack with logarithms to find out what range it's in and act accordingly)

any help would be appreciated,
thor


In reply to Pack/unpack irregularity by thor

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.