简体   繁体   English

在Perl中将十六进制值转换为十六进制字符串

[英]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..). 我以十六进制形式得到数字(如50000000、50000004、50000008、5000000C等。) 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). 我的要求是我应该将其获取为0x50000000,0x50000004,以此类推...为此,我想到了将其转换为十六进制字符串(“ 5000000C”),然后将0x(与。运算符,如0x.5000000C)串联在一起的想法。转换回十六进制值(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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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