The vec() function

S

sonet

my $bitf;
vec($bitf,0,32)= 0x5065726c; #unpack('H*','Perl');
print chr(vec($bitf,0,8)) . chr(vec($bitf,1,8)) . chr(vec($bitf,2,8)) .
chr(vec($bitf,3,8)) , "\n";

If i just know $str='Perl'. How to convert Perl to 0x5065726c?
Have any fast method to convert vec($bitf,0,32) to string 'Perl' ?
 
M

Mumia W. (on aioe)

my $bitf;
vec($bitf,0,32)= 0x5065726c; #unpack('H*','Perl');
print chr(vec($bitf,0,8)) . chr(vec($bitf,1,8)) . chr(vec($bitf,2,8)) .
chr(vec($bitf,3,8)) , "\n";

If i just know $str='Perl'. How to convert Perl to 0x5065726c?
Have any fast method to convert vec($bitf,0,32) to string 'Perl' ?

Do you mean like this?

my $bitf;
vec($bitf,0,32)= 0x5065726c; #unpack('H*','Perl');
print $bitf, "\n";
 
D

Dr.Ruud

sonet schreef:
my $bitf;
vec($bitf,0,32)= 0x5065726c; #unpack('H*','Perl');
print chr(vec($bitf,0,8)) . chr(vec($bitf,1,8)) . chr(vec($bitf,2,8))
. chr(vec($bitf,3,8)) , "\n";

If i just know $str='Perl'. How to convert Perl to 0x5065726c?
Have any fast method to convert vec($bitf,0,32) to string 'Perl' ?

s/Perl/0x5065726c/

perl -wle 'print pack q/H*/, q/5065726c/'
 
S

sonet

if the $a='Perl' and how to let vec($bitf,0,32)=$a;
How to convert 'Perl' to hex and assign the vec($bitf,0,32) equal the
hex("Perl")
 
M

Mirco Wahab

sonet wrote:

Please don't "top post". It looks ugly.
if the $a='Perl' and how to let vec($bitf,0,32)=$a;
How to convert 'Perl' to hex and assign the vec($bitf,0,32) equal the
hex("Perl")

Like here?

...
my $str = 'Perl';
print "\nASC: " . $str;
print "\nDEC: " . join '', unpack "N*", $str;
print "\nHEX: 0x" . join '', unpack "H*", $str;
print "\nBIN: " . join '', unpack "B*", $str;
print "\n";

my $number = unpack "N*", $str;
print "\n" . $number;

my $bitf = 0;
vec( $bitf, 0, 32 ) = $number; # <= 0x50_65_72_6c;
print "\n" . pack "N*", vec $bitf, 0, 32; # "Perl"
...

Regards

M.
 
M

Mumia W. (on aioe)

if the $a='Perl' and how to let vec($bitf,0,32)=$a;
How to convert 'Perl' to hex and assign the vec($bitf,0,32) equal the
hex("Perl")

my $bitf;
vec($bitf,0,32) = unpack 'N*','Perl';
print $bitf;


Read the documentation for pack: perldoc -f pack
 

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

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
FloridaHan

Latest Threads

Top