Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Very Large Hex Combinations

by danambroseUK (Beadle)
on Aug 04, 2005 at 14:15 UTC ( [id://480837]=perlquestion: print w/replies, xml ) Need Help??

danambroseUK has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I have searched the site and found alot of info on various combinations and permutation modules but none seem to do what I am after. I would like to do all the combinations between..

00000000 > FFFFFFFF

i.e

00000001
000A000B
00CCCCD1

But for much larger numbers - I know that there are zillions of combinations. Any help would be great!

Dan

Replies are listed 'Best First'.
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;.

    After Compline,
    Zaxo

      Why so complicated? A simple foreach iterator loop will suffice. Its also the cheapest in runtime.

      printf "%08x\n", $_ for 0 .. ~0;
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"); } );
      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
        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.
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.
Re: Very Large Hex Combinations
by TedYoung (Deacon) on Aug 04, 2005 at 14:45 UTC

    This should get you started...

    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. :-)
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://480837]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-03-19 07:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found