T
Trebor A. Rude
In an effort learn Perl, I was converting some of my smaller C++ programs to
Perl scripts. I had one program which printed out the bytes of an integer
as characters (so 0x616263 would be "abc"). I've got a Perl solution, but
for some reason it seems unnecessarily complicated. Is there a better way
to accomplish the same thing as the script below? I keep thinking that pack
or unpack can do it, but darned if I can figure out how.
#!/usr/bin/perl -w
use strict;
for (@ARGV)
{
$_ = oct if /^0/;
my @bytes;
do { unshift @bytes, $_ & 0xFF; } while (($_ >>= 8) > 0);
print chr for @bytes;
print "\n";
}
Perl scripts. I had one program which printed out the bytes of an integer
as characters (so 0x616263 would be "abc"). I've got a Perl solution, but
for some reason it seems unnecessarily complicated. Is there a better way
to accomplish the same thing as the script below? I keep thinking that pack
or unpack can do it, but darned if I can figure out how.
#!/usr/bin/perl -w
use strict;
for (@ARGV)
{
$_ = oct if /^0/;
my @bytes;
do { unshift @bytes, $_ & 0xFF; } while (($_ >>= 8) > 0);
print chr for @bytes;
print "\n";
}