in reply to Re: Help with pack error
in thread Help with pack error

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.

Replies are listed 'Best First'.
Re^3: Help with pack error
by BrowserUk (Patriarch) on Mar 20, 2012 at 23:41 UTC

    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?

      I wonder if UTF-8 is automatically selected as I don't turn it on at all. Here's the full code.

      #!/usr/bin/perl -w use strict; use LWP; use Getopt::Long; ## ## GLobal variables ## my $primary_server = ""; my $timeout = 30; # No. of seconds to wait for request to timeout my $uagent; # User Agent object my $line; # A Line read from the order file my $mtid; # First 4 digits of the input line my $bits; # Bit flags (64 or 128, first bit of 1 = 128) my $bitsize; # The length of bit flags (either 64 or 128) my $byte; # eight bit flags. my $data; # The data my $reqsize; # The length of the fully packed request. my $request; # Holds the full request line converted to binary my $cmd_line; # Holds the xml command to submit my $reply; # Holds raw reply from server my $error_flag; # Err flag (0=no error, 1=timeout, 2=failed) my $i; # counter my $dummy; ############################################################ ## MAIN main MAIN main MAIN main MAIN main MAIN main MAIN ## ############################################################ # # Read. # #$primary_server = <STDIN>; #$primary_server =~ s/\n//; # # Read the input line from STDIN. # $line = <STDIN>; $line =~ s/\n//; $mtid = substr( $line, 0, 4 ); $reqsize = 4; $bits = substr( $line, 4, 1 ); if ($bits eq "1") { $bitsize = 128; $reqsize += 16; } else { $bitsize = 64; $reqsize += 8; } $bits = substr( $line, 4, $bitsize ); $data = substr( $line, 4 + $bitsize ); $reqsize += length( $data ); $request = pack( "n", $reqsize ); $request .= $mtid; $request .= pack( 'B*', $bits ); $request .= $data; print STDOUT $request;

      Note that this is still a work in progress and several pieces - including the communications and response - have not been coded, yet. I'm just trying to see if I can get this to work for building binary data.

        I wonder if UTF-8 is automatically selected as I don't turn it on at all.

        I'm not aware of any mechanism by which that could happen, but I have little experience in the subject.

        If, for example, you are running in a utf-8 enabled shell and you start perl, does it inherit it standard handles from that shell, and therefore their utf-8ness as well? Dunno, worth asking though.


        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?