Re: Very Large Hex Combinations
by Zaxo (Archbishop) on Aug 04, 2005 at 14:22 UTC
|
To get all those from 0x00000000 to 0xFFFFFFFF, all you need to do is count - for a long time. Assuming 32-bit integers,
my $val = 0|0;
do { printf "%08X\n", $val++ } while $val;
For longer strings, use Math::BigInt;.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
printf "%08x\n", $_
for 0 .. ~0;
| [reply] [Watch: Dir/Any] [d/l] |
Re: Very Large Hex Combinations
by ikegami (Patriarch) on Aug 04, 2005 at 14:57 UTC
|
People have suggested ways of working with numbers. I think it's faster to work with strings in this case (because it reduces greatly the number of calls to sprintf):
my @bytes = map { sprintf('%02X', $_) } 0..255;
foreach my $i (@bytes) {
foreach my $j (@bytes) {
foreach my $k (@bytes) {
foreach my $l (@bytes) {
foreach my $m (@bytes) {
foreach my $n (@bytes) {
print("$i$j$k$l$m$n\n");
}}}}}}
Or you could use NestedLoops in Algorithm::Loops:
use Algorithm::Loops qw( NestedLoops );
my @bytes = map { sprintf('%02X', $_) } 0..255;
my $num_bytes = 6;
NestedLoops(
[ ( [ @bytes ] ) x $bytes ],
sub {
print(join('', @_), "\n");
}
);
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
Thanks all for the *very* quick replies....
Ikegami's idea with using strings seems to be quicker - my next question is how would I expand the bellow code to produce larger hex patterns?
my @bytes = map { sprintf('%02X', $_) } 0..255;
foreach my $i (@bytes) {
foreach my $j (@bytes) {
foreach my $k (@bytes) {
foreach my $l (@bytes) {
foreach my $m (@bytes) {
foreach my $n (@bytes) {
print("$i$j$k$l$m$n\n");
}}}}}}
Many thanks
Dan | [reply] [Watch: Dir/Any] [d/l] |
|
How large? That produces all patterns from 000000000000 to FFFFFFFFFFFF. Add more loops to make it bigger, or increase $num_bytes in my second snippet.
| [reply] [Watch: Dir/Any] |
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Very Large Hex Combinations
by Anonymous Monk on Aug 04, 2005 at 14:50 UTC
|
If you have a 64-bit Perl, a simple
for my $x (0 .. 0x7FFFFFFFFFFFFFFF) {
printf "%016x\n", $x
}
will keep you occupied for 168351 years if you can print a billion lines per second.
For a 32 bit Perl, use something like:
for my $x1 (0 .. 0x7F) {
for my $x2 (0 .. 0xFFFFFFF) {
for my $x3 (0 .. 0xFFFFFFF) {
printf "%02x%07x%07x", $x1, $x2, $x3;
}
}
}
for the same range and running time. | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Very Large Hex Combinations
by TedYoung (Deacon) on Aug 04, 2005 at 14:45 UTC
|
use strict;
use warnings;
use Math::BigInt;
my $x = Math::BigInt->new;
while (++$x) { # Runs forever
print uc substr $x->as_hex, 2;
}
The substr is needed because as_hex puts 0x before the number which you didn't want. And the uc is pretty self-explanitory.
Update: The key point here is that the op wants to go above 2 ** 32 or 2 ** 64. In those cases, I could not get sprintf "%X" to work (without having to do lots of shifting).
Ted Young
($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Very Large Hex Combinations
by Taulmarill (Deacon) on Aug 04, 2005 at 14:30 UTC
|
I know that there are zillions of combinations
no, it are just 2 ** 32 combinations. | [reply] [Watch: Dir/Any] |