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

Hi guys,

Me being a novice in perl,finding hard to sort it out.

$index=0; $start_addr=0x50000000 for(i=$index; $i<256; $i++) { $addr=sprintf("%x",($start_addr=$start_addr + 4)); print "addr +:$addr\n"; }

I get the numbers in their hex form(as 50000000,50000004,50000008,5000000c and so on..)My requirement is that i should get it as 0x50000000,0x50000004 and so on..For that i thought of converting this to a string and concatinating 0x to that and again converting back to hex.But i don't know how to start with.Is there exists a better solution to this?Please throw some light on this.

regards

shyam

Replies are listed 'Best First'.
Re: To convert hex number into string
by ikegami (Patriarch) on Dec 02, 2012 at 12:40 UTC
    sprintf "0x%x"
Re: To convert hex number into string
by Athanasius (Archbishop) on Dec 02, 2012 at 13:03 UTC

    Hello shyam123, and welcome to the Monastery!

    Here is a one-liner that uses ikegami’s solution to accomplish your task:

    perl -we "printf 'addr:0x%x,', 0x50000000 + $_ * 4 for 0 .. 255;"

    (Note that this is for Windows. You will need to adjust the quotes for other platforms.)

    Hope that helps,

    Athanasius <°(((><contra mundum