extracst the first 8 bits with pack

P

perlistpaul

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";
 
A

anno4000

perlistpaul said:
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
 
P

paul

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top