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

Help with pack error

by taack (Initiate)
on Mar 20, 2012 at 22:19 UTC ( [id://960672]=perlquestion: print w/replies, xml ) Need Help??

taack has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use pack to create an ISO8583 set of bit flags. But, I'm running into a problem whenever the most significant bit is set. For instance, pack( "B8", "10000000" ) yields "\302 \200" rather than "\200", pack( "B8", "11000000" ) yields "\303 \200" rather than "\300". Is this a bug in the pack routine or am I missing something in the format string?

Any help would be appreciated.

Replies are listed 'Best First'.
Re: Help with pack error
by BrowserUk (Patriarch) on Mar 20, 2012 at 22:32 UTC
    But, I'm running into a problem whenever the most significant bit is set....

    How are you determining what pack is producing?

    Cos I'm seeing no signs of what you describe:

    $s = pack 'B8', '10000000';; print length $s;; 1 print ord( $s );; 128 $s = pack 'B8', '11000000';; print length $s;; 1 print ord( $s );; 192

    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".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Actually, I'm sending it through pack and then printing it to STDOUT and then piping the result to od. (Didn't think to use ord. Oh well, lots to learn.)

      In any case, 16 bytes of data are produced by pack (pack( "B128", $bits ) and all come back fine except any byte with the 8th bit set.

      I haven't got the version number in front of me, but it is an older version of Perl running on Red Hat. Maybe that's the problem.

        Do you have some form of encoding enabled on stdout?

        That's the only way I can reproduce your findings:

        C:\test>perl -e"print pack 'B8', '11000000';" | od -t o1 0000000 300 0000001 C:\test>perl -CO -e"print pack 'B8', '11000000';" | od -t o1 0000000 303 200 0000002

        Note: -CO means set STDOUT to utf-8


        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".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Re: Help with pack error
by JavaFan (Canon) on Mar 21, 2012 at 07:36 UTC
    Don't use UTF-8
    $ perl -MDevel::Peek -E '$x = pack("B8", "10000000"); Dump($x); utf8:: +upgrade($x); Dump($x)' SV = PV(0x923c0dc) at 0x924ebb8 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x9253a80 "\200"\0 CUR = 1 LEN = 4 SV = PV(0x923c0dc) at 0x924ebb8 REFCNT = 1 FLAGS = (POK,pPOK,UTF8) PV = 0x9253a80 "\302\200"\0 [UTF8 "\x{80}"] CUR = 2 LEN = 4
Re: Help with pack error
by taack (Initiate) on Mar 21, 2012 at 15:51 UTC

    Wait! I just figured it out. I'm being stupid. I just realized that I didn't put STDOUT in binmode before attempting to look at what pack had wrought.

    I'm a little slow, but I can be taught!

    Thanks for all your help.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-03-28 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found