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

Hello, So I have a script written to do a simple conversion of taking user input in the format of ##:## where the # are in place of a of a number of 0-9 or a letter of A-F (exception of not having the letter E in there) and calculating its hex value then adding one to it, and repeating for a amount specified by the user. The section of code relevant is
#takes the first two characters and stores them for later $AddressOriginal = substr($Address, 0, 3); #removes the first three characters leaving the later ## in the format + ##:## $Address = substr($Address, 3, 2); #Test lines to make sure the above lines do their intended purpose #print $Address . "\n"; #print $AddressOriginal . "\n"; for($count = 1; $count <= $IDcount; $count++) { $hexval = sprintf("%02X\n", ($Address)); $Address++; $Address1 = ($AddressOriginal . $hexval); #Test line to make sure the procedure completed its intended task print $Address1;
I have encountered a bug where when the user puts in a value with a letter in it of the second half of ##:##, it starts at 00. For example, Say I go and I pass it 3F:13, the next iteration would be 3F:14, 3F:15, 3F:16... However say I pass it A7:F0, we would expect it to go A7:F1, A7:F2, A7:F3... however it instead goes A7:00, A7:01, A7:02... I have already did several google searches, and looked into perldocs, however I am having trouble finding anything to be able to help me. The closest I have found is that perl assumes that whenever a user puts in a character such as F or B, that it makes it a string. I have already tried, having it be taken more clearly as a decimal I believe, by having the user put in two separate values, but nothing changed. Could anyone please point me in the right direction on where I may be going wrong, or explain to me where I have messed up?

Replies are listed 'Best First'.
Re: Invalid Hex Conversion?
by toolic (Bishop) on Jul 21, 2011 at 18:54 UTC
    use strict and warnings:
    use diagnostics; my $Address = 'A7:F0'; #takes the first two characters and stores them for later $AddressOriginal = substr($Address, 0, 3); #removes the first three characters leaving the later ## in the format + ##:## $Address = substr($Address, 3, 2); print "$Address\n"; #Test lines to make sure the above lines do their intended purpose #print $Address . "\n"; #print $AddressOriginal . "\n"; #for($count = 1; $count <= $IDcount; $count++) { $hexval = sprintf("%02X\n", ($Address)); $Address++; $Address1 = ($AddressOriginal . $hexval); #Test line to make sure the procedure completed its intended task print $Address1; #} __END__ F0 Argument "F0" isn't numeric in sprintf at ... (#1) (W numeric) The indicated string was fed as an argument to an oper +ator that expected a numeric value instead. If you're fortunate the me +ssage will identify which operator was so unfortunate. A7:00
    See also:
Re: Invalid Hex Conversion?
by JavaFan (Canon) on Jul 21, 2011 at 18:52 UTC
    While postincrement is magical, it does not recognize strings containing hex representations of numbers.

    You should convert your (string) input to numbers, increment the number, and only convert back to hex when displaying. Do not try to do arithmetic on strings containing hex representations of numbers.

Re: Invalid Hex Conversion?
by Anonymous Monk on Jul 21, 2011 at 18:39 UTC

    Where in your code do you convert the hex string into a number that can be incremented properly?

      I increment $Address in the second line of the for loop with $Address++. Is this an improper way to do this? Because the script works fine, up to the point where you pass it a character, then it produces the bug I described.

        Incrementing is magical, but not THAT magical :)

        There is no way to tell if you want the increment to change "19" into "1A" rather than "20", for example.

        Use $value = hex($string) to get a regular number before you apply math to it.