in reply to Hex to decimal resulting in integer overflow

Just build your own function to increment theid's. Won't have to worry about any out of bounds issues then.
use strict; my $hex = "24d0e803adb7"; for (0..16) { $hex = inc_hex($hex); print $hex, "\n"; } sub inc_hex { my @a = reverse map {hex} split '', $_[0]; $a[my $i = 0]++; while ($a[$i] == 16) { $a[$i] = 0; $a[++$i]++; } return join '', map {sprintf "%x", $_} reverse @a; }
output
24d0e803adb8 24d0e803adb9 24d0e803adba 24d0e803adbb 24d0e803adbc 24d0e803adbd 24d0e803adbe 24d0e803adbf 24d0e803adc0 24d0e803adc1 24d0e803adc2 24d0e803adc3 24d0e803adc4 24d0e803adc5 24d0e803adc6 24d0e803adc7 24d0e803adc8

Replies are listed 'Best First'.
Re^2: Hex to decimal resulting in integer overflow (inc hex regex)
by tye (Sage) on Apr 01, 2011 at 22:50 UTC

    You can also do that with a single regex substitution.

    BEGIN { my %n= ( 0..9, 'a'..'f', 1..9, 'a'..'f', 10 ); sub incHex { s{ ( [0-9a-fA-F] ) # Any hex digit followed by ( [fF]* )\b # (always) trailing (optional) 'f's }{ $n{ lc $1 } . '0' x length($2); }gex for @_; } }

    - tye        

      Cool, thanks for sharing. I was actually looking for that solution, but just got a tooth extracted today and the brain just wasn't working right.

      The 'g' modifier isn't actually necessary there, and adding 'i' lets you remove the upper case entries from the character classes. Of course, the hash lookup is as fast as you're ever going to get, but I'd still probably choose to do the functional solution.

      sub incHex { s{ ( [0-9a-f] ) # Any hex digit followed by ( f* )\b # (always) trailing (optional) 'f's }{ sprintf("%x", 1 + hex $1) . '0' x length($2); }iex for @_; }

      Very nice though

        Actually, the /g was quite intentional. If you are going to drop that, you might also want to change the \b to a $.

        Yes, my hash look-up is likely insignificantly faster. But your use of /i also likely leads to an additional (insignificant) performance penalty.

        Perhaps somewhat unfortunately, the computer science term "functional" means much more than "using functions". Perhaps you should use "procedural" instead? Or "function-based"?

        Thanks.

        - tye