对于某些字符串,需要输入为特定的格式,通过sprintf可以很方便的完成,不需要专门进行其他处理。
perl中的sprintf的用法如下:
字符串变成相应的数字eval
sprintf FORMAT, LIST
$num2
print "the num2 zero value is $num2\n";
$num2
print "the num2 second value is $num2\n";
$num2 = sprintf("%x",$num2);
print "the num2 third value is $num2\n";
$num0=0x4e8>>2;
print "the num0 value is $num0\n";
print "Generate regfile.v successfully!\n";
#$BaseAddress = "efH";
#$Offset
#$BaseAddress =~ s/H//;
#$Offset
#$addr_init = "0x$BaseAddress$Offset";
$addr_init = "0xefe";
$addr_ch
print "Generate $addr_ch successfully!\n";
比如:
$result = sprintf("d",$number);让$number有8个前导零。
$rounded = sprintf("%.3f",$number);
让小数点后有3位数字。
sprintf允许的如下常用的转换:
%%
%c
%s
%d
%u
%o
%x
%e
%f
%g
通过$1,$2等可以改变顺序:
printf '<% d>',
printf '<%+d>',
printf '<%6s>',
printf '<%-6s>', 12;
printf '<s>', 12;
printf '<%#o>',
printf '<%#x>',
printf '<%#X>',
printf '<%#b>',
printf '<%#B>',
printf '<%.6d>', 1;
printf '<%+.6d>', 1;
printf '<%-10.6d>', 1;
printf '<.6d>', 1;
printf '<0.6d>', 1;
printf '<%+10.6d>', 1;
printf '<%.6x>', 1;
printf '<%#.6x>', 1;
printf '<%-10.6x>', 1;
printf '<.6x>', 1;
printf '<0.6x>', 1;
printf '<%#10.6x>', 1;
printf '<%.5s>', "truncated";
printf '<.5s>', "truncated"; # prints "<
"
4/n"
4/n"
n"