extracst the first 8 bits with pack

Discussion in 'Perl Misc' started by perlistpaul, Aug 22, 2006.

  1. perlistpaul

    perlistpaul Guest

    Hello everyone,

    my purpose of the code is to do the following, put number into array,
    and extracts the first 8bits from each stored number, interprets there
    8bits chunks as characters.

    Isn't it correct?
    --------------------------------------
    for(@bitplay)
    {
    $count++;
    print pack("A6x5A1x1A1x3 s","Number","#",$count,$_),"\n";

    }

    my $str=pack("lll",@bitplay);

    print "String is ". $str."\n";

    ----------------

    Thank you

    paul
     
    perlistpaul, Aug 22, 2006
    #1
    1. Advertising

  2. perlistpaul

    -berlin.de Guest

    perlistpaul <> wrote in comp.lang.perl.misc:
    > Hello everyone,
    >
    > my purpose of the code is to do the following, put number into array,


    There is only one array, @bitplay, in your code. Nothing puts a
    number in there, your code assumes it is already set. That is
    unfortunate because we don't know what you assume it is set to.

    > and extracts the first 8bits from each stored number, interprets there
    > 8bits chunks as characters.


    By the "first" eight bits you presumably mean the lowest-numbered byte
    in the four byte sequence that make up the number. You are aware that
    the content of that byte will depend on the architecture (little/big
    endian) the program runs on.

    > Isn't it correct?


    Is what correct? Your code shows two different ways of extracting
    things from @bitplay and prints them. Both show no relationship
    to what you described above.

    > --------------------------------------
    > for(@bitplay)
    > {
    > $count++;
    > print pack("A6x5A1x1A1x3 s","Number","#",$count,$_),"\n";
    >
    > }
    >
    > my $str=pack("lll",@bitplay);
    >
    > print "String is ". $str."\n";


    This may get you started:

    my $n = 0x61626364; # "abcd"
    my $first_byte = substr pack( 'l', $n), 0, 1;
    print "$first_byte\n"; # "d" on little endian, "a" on big endian

    Anno
     
    -berlin.de, Aug 22, 2006
    #2
    1. Advertising

  3. perlistpaul

    paul Guest

    my code let user to input from the keyboard
    for example user key in : 65 67 68 69

    the code must be able to extract the first 8bits from each stored
    number
    and interprets there 8bits chunks as characters.

    I don't see that your code suitable for what the requirement and as
    what I already provided. Any method?

    Thank

    -berlin.de wrote:
    > perlistpaul <> wrote in comp.lang.perl.misc:
    > > Hello everyone,
    > >
    > > my purpose of the code is to do the following, put number into array,

    >
    > There is only one array, @bitplay, in your code. Nothing puts a
    > number in there, your code assumes it is already set. That is
    > unfortunate because we don't know what you assume it is set to.
    >
    > > and extracts the first 8bits from each stored number, interprets there
    > > 8bits chunks as characters.

    >
    > By the "first" eight bits you presumably mean the lowest-numbered byte
    > in the four byte sequence that make up the number. You are aware that
    > the content of that byte will depend on the architecture (little/big
    > endian) the program runs on.
    >
    > > Isn't it correct?

    >
    > Is what correct? Your code shows two different ways of extracting
    > things from @bitplay and prints them. Both show no relationship
    > to what you described above.
    >
    > > --------------------------------------
    > > for(@bitplay)
    > > {
    > > $count++;
    > > print pack("A6x5A1x1A1x3 s","Number","#",$count,$_),"\n";
    > >
    > > }
    > >
    > > my $str=pack("lll",@bitplay);
    > >
    > > print "String is ". $str."\n";

    >
    > This may get you started:
    >
    > my $n = 0x61626364; # "abcd"
    > my $first_byte = substr pack( 'l', $n), 0, 1;
    > print "$first_byte\n"; # "d" on little endian, "a" on big endian
    >
    > Anno
     
    paul, Aug 23, 2006
    #3
    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. GGG
    Replies:
    10
    Views:
    12,635
    Donar
    Jul 6, 2006
  2. Tim Jones
    Replies:
    0
    Views:
    395
    Tim Jones
    Jan 31, 2004
  3. sarmin kho
    Replies:
    2
    Views:
    840
    A. Lloyd Flanagan
    Jun 15, 2004
  4. Aaron Brice

    pack/unpack bits

    Aaron Brice, Aug 6, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    131
    Kien Ha
    Aug 6, 2003
  5. Alexander Farber

    pack 'C3U*' not same as pack 'C3(xC)*'

    Alexander Farber, Jun 23, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    152
    Ilmari Karonen
    Jun 23, 2005
Loading...

Share This Page