简体   繁体   中英

To convert a hex value to a hex string in Perl

Consider:

$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 hex string ("5000000C") and concatenating 0x(with . operator like 0x.5000000C) to that and again converting back to hex value (0x5000000C). But I am not getting how to start with. Does there exist a better solution to this?

只需将0x添加到sprintf模式:

sprintf("0x%X",($start_addr = $start_addr + 4));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM