Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: Using pack to evaluate text strings as hexadecimal values

by BrowserUk (Pope)
on Mar 21, 2011 at 18:26 UTC ( #894593=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Using pack to evaluate text strings as hexadecimal values
in thread Using pack to evaluate text strings as hexadecimal values

What doesn't this do that you'd like it to do?

$n = 0; $n |= 1 << $_ for 1,3,5,7;; print $n, unpack 'H2', pack 'v', $n;; 170 aa

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re^3: Using pack to evaluate text strings as hexadecimal values
Download Code
Re^4: Using pack to evaluate text strings as hexadecimal values
by jpl (Monk) on Mar 22, 2011 at 11:56 UTC

    It doesn't give me a single format string that I can use to convert back and forth between "records" and "fields".

    @fields = unpack($format, $record); $record = pack($format2, @fields);

    (I actually use hash slices rather than arrays, but that's not germane here.) I don't mind having distinct formats. For all-alpha fields, I sometimes do

      ($format3 = $format) =~ s/A/a/g;

    so I can generate a non-trimming unpack format from the default, a convenience, but not absolutely essential, since setting up the formats is a one-time thing, but conversion is a per-record thing.

      It doesn't give me a single format string that I can use to convert back and forth between "records" and "fields".

      The "problem" is that you cannot have a single conversion that produces binary numeric data for internal math use, and also produces asciified hex string for display purposes.

      It's the same as asking for a single printf format that will display an number as decimal and hex. It cannot be done.

      The solution is to have two templates: 1 for internal packing and unpacking:

      $templ = 'A4 v d';; @fields = ( 'fred', 12345, 123.456 );; $record = pack $templ, @fields;; print unpack $templ, $record;; fred 12345 123.456

      And another for unpack a record for display purposes:

      $templ2 = 'A4 H4 H16';; print unpack $templ2, $record;; fred 3930 77be9f1a2fdd5e40

      There is simply no way to implement a single template that would server both purposes.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Perhaps I am missing something. Why would it be "impossible" to have a format item "Y" (where "Y" is some unused format character, finding which may be the real problem) such that

        pack("Y4", 32); # produces "0020" unpack("Y4", "0020"); # produces 32

        There are many format characters, v among them, that pack integers into strings and unpack strings into integers. I believe the only practical difference between "Y" and "v" is the contents of the string. The one produced by "v" may be unsuitable for display, the one produced by "Y" would be both displayable and, when displayed, indicative of the bits in the integer from which it was produced.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://894593]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2014-07-13 13:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (249 votes), past polls