Perl笔记
来自Jack's Lab
(版本间的差异)
(→位反转) |
(→32位位反转) |
||
| 第31行: | 第31行: | ||
d = 0x80008001 | d = 0x80008001 | ||
rd = 0x80010001 | rd = 0x80010001 | ||
| + | </source> | ||
| + | |||
| + | <br><br> | ||
| + | |||
| + | == 进制转换 == | ||
| + | |||
| + | For bash: | ||
| + | |||
| + | <source lang=bash> | ||
| + | # for bash. your shell is probably different. | ||
| + | |||
| + | alias d2h="perl -e 'printf qq|%X\n|, int( shift )'" | ||
| + | alias d2o="perl -e 'printf qq|%o\n|, int( shift )'" | ||
| + | alias d2b="perl -e 'printf qq|%b\n|, int( shift )'" | ||
| + | |||
| + | alias h2d="perl -e 'printf qq|%d\n|, hex( shift )'" | ||
| + | alias h2o="perl -e 'printf qq|%o\n|, hex( shift )'" | ||
| + | alias h2b="perl -e 'printf qq|%b\n|, hex( shift )'" | ||
| + | |||
| + | alias o2h="perl -e 'printf qq|%X\n|, oct( shift )'" | ||
| + | alias o2d="perl -e 'printf qq|%d\n|, oct( shift )'" | ||
| + | alias o2b="perl -e 'printf qq|%b\n|, oct( shift )'" | ||
</source> | </source> | ||
2014年12月25日 (四) 08:35的版本
目录 |
1 Catalyst 安装
$ apt-get install perl perl-modules libcatalyst-perl libcatalyst-modules-perl
2 32位位反转
$ cat bit-rev.pl
#!/usr/bin/perl
sub rev
{
my $in = shift;
$in = ($in & 0x55555555) << 1 | ($in & 0xaaaaaaaa) >> 1;
$in = ($in & 0x33333333) << 2 | ($in & 0xCCCCCCCC) >> 2;
$in = ($in & 0x0f0f0f0f) << 4 | ($in & 0xf0f0f0f0) >> 4;
$in = ($in & 0x00ff00ff) << 8 | ($in & 0xff00ff00) >> 8;
$in = ($in & 0x0000ffff) << 16 | ($in & 0xffff0000) >> 16;
return $in;
}
my $d = 0x80008001;
printf "d = 0x%08x\n", $d;
printf "rd = 0x%08x\n", &rev($d);
$ ./bit-rev.pl
d = 0x80008001
rd = 0x80010001
3 进制转换
For bash:
# for bash. your shell is probably different. alias d2h="perl -e 'printf qq|%X\n|, int( shift )'" alias d2o="perl -e 'printf qq|%o\n|, int( shift )'" alias d2b="perl -e 'printf qq|%b\n|, int( shift )'" alias h2d="perl -e 'printf qq|%d\n|, hex( shift )'" alias h2o="perl -e 'printf qq|%o\n|, hex( shift )'" alias h2b="perl -e 'printf qq|%b\n|, hex( shift )'" alias o2h="perl -e 'printf qq|%X\n|, oct( shift )'" alias o2d="perl -e 'printf qq|%d\n|, oct( shift )'" alias o2b="perl -e 'printf qq|%b\n|, oct( shift )'"
4 Resource
- Mastering Perl: http://chimera.labs.oreilly.com/books/1234000001527/index.html
- Catalyst Manual: https://metacpan.org/release/Catalyst-Manual
- meta cpan: https://metacpan.org/pod/Data::Random
- perl doc: http://perldoc.perl.org/