Perl笔记
来自Jack's Lab
(版本间的差异)
(→进制转换) |
小 (→十六进制字符串) |
||
第80行: | 第80行: | ||
id = 0x2646ca4a | id = 0x2646ca4a | ||
decode id = 0x52536264 | decode id = 0x52536264 | ||
+ | </source> | ||
+ | |||
+ | <br><br> | ||
== Resource == | == Resource == |
2014年12月25日 (四) 09:32的版本
目录 |
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 十六进制字符串
$ cat test.pl my $seed = 0x52536264; print "seed = " . $seed . "\n"; printf "seed = 0x%x\n", $seed; # bit revert $seed = &rev($seed); printf "seed bit revert = 0x%x\n", $seed; my $id = sprintf("0x%08x", $seed); printf "id = %s\n", $id; printf "decode id = 0x%x\n", &rev(hex $id); $ ./test.pl seed = 1381196388 seed = 0x52536264 seed bit revert = 0x2646ca4a id = 0x2646ca4a decode id = 0x52536264
5 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/