我有一个 file.txt
文件,每行都是一个十进制数:
1
2
3
我尝试(已经很长时间了)编写一个一行脚本,以便输出每行都有十进制、十六进制和二进制的列。为了简化任务,我们可以假设原始数字是用一个字节表示的。因此,最大值是255。
I first try to decode each number as a bynary with prepended 0 so to have an 8 bits pattern:
awk '{print "ibase=10;obase=2;" $1}' $1 | bc | xargs printf "%08d\n"
where the outer $1 in the awk statement is
file.txt
. The output is :00000001 00000010 00000011
Same for hex with one prepended 0
awk '{printf("0x%02x\n", $1)}' $1
Same as before. The Output is :
0x01 0x02 0x03
Well, the decimal should be just a print:
1 2 3
我想要的是一行代码:
1 00000001 0x01
2 00000001 0x02
基本上,要在输出的每一行中放置1、2和3。
我尝试使用system()在awk中执行bc(和其他命令),但没有成功。还有其他无数种方法。你会怎么做?
printf "%X" 15
,它将返回F
。 - fedorqui