目录
1. 使用sprintf, printf
1.1: 10进制 -> 非10进制
my $num = 10;
my $s_hex_low = sprintf "%04x", $num; # 000a, 10进制->16进制小写
my $s_hex_high= sprintf "%04X", $num; # 000A, 10进制->16进制大写
my $s_oct = sprintf "%04o", $num; # 0012, 10进制->8进制
my $s_bin = sprintf "%04b", $num; # 1010, 10进制->2进制
#print("$s_hex_low\n");
#print("$s_hex_high\n");
#print("$s_oct\n");
#print("$s_bin\n");
1.2: 非10进制 -> 10进制
my $n_hex = 0x10; # 16进制数
my $n_oct = 010 ; # 8进制数
my $n_bin = 0b10; # 2进制数
my $s_hex2dec = sprintf "%04d", $n_hex; # 0016, 16进制->10进制
my $s_oct2dec = sprintf "%04d", $n_oct; # 0008, 8进制->10进制
my $s_bin2dec = sprintf "%04d", $n_bin; # 0002, 2进制->10进制
#print("$s_hex2dec\n");
#print("$s_oct2dec\n");
#print("$s_bin2dec\n");
2. 使用函数oct, hex
2.1 非10进制->10进制
my $s = "0010";
my $n_hex2dec = oct("0x".$s); # 16, 16进制->10进制
my $n_oct2dec = oct("0" .$s); # 8, 8进制->10进制
my $n_bin2dec = oct("0b".$s); # 2, 2进制->10进制
#print("$n_hex2dec\n");
#print("$n_oct2dec\n");
#print("$n_bin2dec\n");
print oct(10); # 8, 8进制->10进制
print hex(10); #16, 16进制->10进制
标签:10,进制,笔记,Perl,sprintf,print,my,oct
From: https://www.cnblogs.com/gaiqingfeng/p/17556617.html