将十进制转换为四位十六进制数

时间:2015-08-10 16:54:03

标签: perl

有没有办法将十进制数转换为四位十六进制数?例如:

Decimal: 17    =>   Hex: 0x0011 
Decimal: 291   =>   Hex: 0x0123
Decimal: 4951  =>   Hex: 0x1357

基本上,我想保留前导零。

到目前为止,我有:

my $HEX_NUM = sprintf("%x", $DEC_NUM);

有没有办法在没有硬编码的情况下获得前导零?

1 个答案:

答案 0 :(得分:4)

在printf说明符中使用0 flagwidth

var data = [
            {date: "01/01/2013", Ticket: 'bugzilla-12', status: closed, type: defect},
            {date: "01/01/2013", Ticket: 'bugzilla-13', status: closed, type: enhancement},
            {date: "01/02/2013", Ticket: 'bugzilla-14', status: open, type: defect},
            {date: "01/02/2013", Ticket: 'bugzilla-15', status: open, type: feature},
            {date: "01/02/2013", Ticket: 'bugzilla-16', status: onhold, type: feature},
            {date: "01/02/2013", Ticket: 'bugzilla-17', status: closed, type: enhanncement},
            {date: "01/03/2013", Ticket: 'bugzilla-18', status: open, type: defect}
        ];

虽然从您的示例中,您可能还需要“#”标志(但您必须将宽度增加到6才能容纳0x)。