Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: pack() v unpack() padding: bug or ???

by Athanasius (Archbishop)
on Feb 16, 2015 at 16:11 UTC ( [id://1116886]=note: print w/replies, xml ) Need Help??


in reply to pack() v unpack() padding: bug or ???

Hello BrowserUk,

As I understand it, pack (A2)*, LIST says: for each element in LIST, convert it to a 2-byte ASCII string, padded with a trailing space character if necessary; then return the concatenation of these converted elements as a single string.

Logically, then, since unpack “does the reverse of pack”, unpack (A2)*, STRING should break STRING into 2-character chunks, and for each chunk, first remove any trailing spaces, then convert it to an appropriate value. Which is what I see:

2:03 >perl -MData::Dump -wE "my @c = unpack '(A2)*', 'f 12dx z'; dd \ +@c;" ["f", 12, "dx", " z"] 2:04 >

Note that "f " has been converted to "f" by removal of the trailing space (padding).

print unpack 'C*', pack '(Z2)*', 'fr','ed','x';; 102 0 101 0 120 0 ## Note! the 'r' & 'd' characters have been thrown +away? Another bug?

I don’t see a problem here, either. The template Z2 specifies a 2-byte null-terminated string. Packing 'fr' of course requires 3 bytes, so the second character is thrown away and the packed result is f\0. Isn’t that what it’s supposed to do?

Hope that helps (and I apologise if I’ve missed the point!),

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: pack() v unpack() padding: bug or ???
by BrowserUk (Patriarch) on Feb 16, 2015 at 16:32 UTC
    I don’t see a problem here, either. The template Z2 specifies a 2-byte null-terminated string. Packing 'fr' of course requires 3 bytes, so the second character is thrown away and the packed result is f\0. Isn’t that what it’s supposed to do?

    Can you think of a good use for that? Or even a not good use?

    Mentioned in the docs is "Z A null-terminated (ASCIZ) string, will be null padded.". Wouldn't you expect it to grab the 2 characters and then add the null byte (if required)?

    As is, it makes: pack 'Z1', $anything; a very complicated why of generating a null.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

      BrowserUk:

      Since it specifies ASCIZ, I'd expect it to work like it does. Isn't 'a2' the behaviour you're asking for?

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        Isn't 'a2' the behaviour you're asking for?

        It depends whether you mean the behaviour I'm looking for in the OP; or the behaviour I expect from Z; but actually, the answer is no in both cases.

        It's not a substitute for (my expected behaviour of) Z because it doesn't "null terminate", it only "null pads":

        C:\test>p1 print unpack 'C*', pack 'a2', $_ for qw[ fr x ];; 102 114 120 0

        And my OP question relates to unpack rather than pack. Athanasius picked up on a comment in a code sample that was more of a reminder to myself rather than a part of the OP.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        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". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-19 10:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found