unpack vs. split problem (really weird)

Discussion in 'Perl Misc' started by Mike Hunter, Nov 24, 2003.

  1. Mike Hunter

    Mike Hunter Guest

    Hey everybody,

    I'm having a weird problem. Check this out:

    sub mth_inet_ntoa
    {
    my $stuff = shift;
    $debug and print "mth_inet_ntoa_debug length \$stuff: ".(length $stuff)."\n";
    my @o = split //, $stuff;
    $debug and print "mth_inet_ntoa debug lengths ".
    (length $o[0])." ".(length $o[1])." ".(length $o[2])." ".(length $o[3])."\n";
    $debug and print "mth_inet_ntoa debug ".(join "", (unpack "b*", $stuff))."\n";
    $debug and print "mth_inet_ntoa debug #".(join "", unpack "b*", $o[0]).
    "#".(join "", unpack "b*", $o[1])."#".
    (join "", unpack "b*", $o[2])."#".(join "", unpack "b*", $o[3])."#\n";
    return "$o[0].$o[1].$o[2].$o[3]";
    }

    mth_inet_ntoa_debug length $stuff: 4
    mth_inet_ntoa debug lengths 1 1 1 1
    mth_inet_ntoa debug 00000001000001001010110000000000
    mth_inet_ntoa debug #00000001#00000100#10101100#00000000#

    Now, if I change from

    split //, $stuff

    to

    unpack "CCCC", $stuff

    I get these totally weirded-out results:

    mth_inet_ntoa_debug length $stuff: 4
    mth_inet_ntoa debug lengths 3 2 2 1
    mth_inet_ntoa debug 00000001000001001010110000000000
    mth_inet_ntoa debug #100011000100110000011100#1100110001001100#1010110011001100#00001100#

    perl -V:

    Summary of my perl5 (revision 5.0 version 8 subversion 0) configuration:
    Platform:
    osname=freebsd, osvers=4.8-release, archname=i386-freebsd
    ...

    Shouldn't unpack "CCCC" work?

    Mike
    Mike Hunter, Nov 24, 2003
    #1
    1. Advertising

  2. Mike Hunter wrote:
    >
    > I'm having a weird problem. Check this out:
    >
    > [snip]
    >
    > mth_inet_ntoa_debug length $stuff: 4
    > mth_inet_ntoa debug lengths 1 1 1 1
    > mth_inet_ntoa debug 00000001000001001010110000000000
    > mth_inet_ntoa debug #00000001#00000100#10101100#00000000#
    >
    > Now, if I change from
    >
    > split //, $stuff
    >
    > to
    >
    > unpack "CCCC", $stuff
    >
    > I get these totally weirded-out results:
    >
    > mth_inet_ntoa_debug length $stuff: 4
    > mth_inet_ntoa debug lengths 3 2 2 1
    > mth_inet_ntoa debug 00000001000001001010110000000000
    > mth_inet_ntoa debug #100011000100110000011100#1100110001001100#1010110011001100#00001100#
    >
    > [snip]
    >
    > Shouldn't unpack "CCCC" work?


    The equivalent of:

    my @o = split //, $stuff

    Is:

    my @o = unpack 'a' x length $stuff, $stuff


    The equivalent of:

    my @o = unpack 'CCCC', $stuff

    Is:

    my @o = map ord, split //, $stuff



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Nov 24, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amir
    Replies:
    3
    Views:
    594
  2. hilz
    Replies:
    6
    Views:
    1,446
  3. nc
    Replies:
    1
    Views:
    493
    nice.guy.nige
    Feb 3, 2005
  4. holgeradelmann

    [Q] really WEIRD asp problem (XP, IIS, IE 6)

    holgeradelmann, Jul 24, 2003, in forum: ASP General
    Replies:
    2
    Views:
    108
    holgeradelmann
    Aug 21, 2003
  5. will
    Replies:
    6
    Views:
    407
    Phrogz
    Dec 27, 2006
Loading...

Share This Page