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

Does anyone know why I'm getting this error:
Illegal hexadecimal digit ' ' ignored at G:\Beta\Trap_Tool\hex.pl line 9.
from the following code:
#!/usr/bin/perl use warnings; use strict; my $num = "0x10000000"; for (0..5) { my $dec_num = sprintf("%d", hex("$num")); print "DEC: $dec_num\n"; $dec_num ++; my $hex_num = sprintf "0x" . "%x\n", $dec_num; print "HEX: $hex_num\n"; $num = "$hex_num"; }

Replies are listed 'Best First'.
Re: Illegal hexadecimal digit error
by Abigail-II (Bishop) on Jul 07, 2003 at 14:40 UTC
    The sprintf that assigns to $hex_num contains a newline. Newlines aren't hex numbers, just as the warning says.

    Abigail

(jeffa) Re: Illegal hexadecimal digit error
by jeffa (Bishop) on Jul 07, 2003 at 14:40 UTC
    That's a warning, not an error ... it results from trying to convert a hex 'number' which has a newline in it. Remove the newline:
    my $hex_num = '0x' . sprintf('%x', $dec_num);
    Also, don't use quotes when you copy $hex_num to $num at the end of the loop (it doesn't make a difference here, but it is a bad habit to get into).

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Or even better, put it all in sprintf:
      my $hex_num = sprintf("0x%x", $dec_num);
Re: Illegal hexadecimal digit error
by dws (Chancellor) on Jul 07, 2003 at 16:37 UTC
    Note that you start getting this warning on the second trip through the loop. The first time through, $num gets a newline tacked onto the end of it. Remove the \n in the second sprintf, and the problem goes away.