Awk 将十进制转换为二进制
我想使用AWK将文件中的十进制数字列表转换为二进制,但似乎没有内置方法。示例文件如下所示:Awk 将十进制转换为二进制,awk,binary,decimal,Awk,Binary,Decimal,我想使用AWK将文件中的十进制数字列表转换为二进制,但似乎没有内置方法。示例文件如下所示: 134218506 134218250 134217984 1610612736 16384 33554432 您不应为此使用awk,但应使用bc: 或 您不应为此使用awk,但应使用bc: 或 您可以尝试Perl一行程序 $ cat hamdani.txt 134218506 134218250 134217984 134217984 1610612736 16384 33554432 $ pe
134218506
134218250
134217984
1610612736
16384
33554432
您不应为此使用awk,但应使用bc:
或
您不应为此使用awk,但应使用bc:
或
您可以尝试Perl一行程序
$ cat hamdani.txt
134218506
134218250
134217984
134217984
1610612736
16384
33554432
$ perl -nle ' printf("%b\n",$_) ' hamdani.txt
1000000000000000001100001010
1000000000000000001000001010
1000000000000000000100000000
1000000000000000000100000000
1100000000000000000000000000000
100000000000000
10000000000000000000000000
$
您可以尝试Perl一行程序
$ cat hamdani.txt
134218506
134218250
134217984
134217984
1610612736
16384
33554432
$ perl -nle ' printf("%b\n",$_) ' hamdani.txt
1000000000000000001100001010
1000000000000000001000001010
1000000000000000000100000000
1000000000000000000100000000
1100000000000000000000000000000
100000000000000
10000000000000000000000000
$
这是一个awk方式,功能化为您的乐趣:
awk '
function d2b(d, b) {
while(d) {
b=d%2b
d=int(d/2)
}
return(b)
}
{
print d2b($0)
}' file
前三条记录的输出:
1000000000000000001100001010
1000000000000000001000001010
1000000000000000000100000000
这是一个awk方式,功能化为您的乐趣:
awk '
function d2b(d, b) {
while(d) {
b=d%2b
d=int(d/2)
}
return(b)
}
{
print d2b($0)
}' file
前三条记录的输出:
1000000000000000001100001010
1000000000000000001000001010
1000000000000000000100000000
您可以尝试使用dc:
# -f infile : Use infile for data
# after -e , it is there are the dc command
dc -f infile -e '
z # number of values
sa # keep in register a
2
o # set the output radix to 2 : binary
[
Sb # keep all the value of infile in the register b
# ( b is use here as a stack)
z
0 <M # until there is no more value
] sM # define macro M in [ and ]
lMx # execute macro M to populate stack b
[
Lb # get all values one at a time from stack b
p # print this value in binary
la # get the number of value
1
- # decremente it
d # duplicate
sa # keep one in register a
0<N # the other is use here
]sN # define macro N
lNx' # execute macro N to print each values in binary
您可以尝试使用dc:
# -f infile : Use infile for data
# after -e , it is there are the dc command
dc -f infile -e '
z # number of values
sa # keep in register a
2
o # set the output radix to 2 : binary
[
Sb # keep all the value of infile in the register b
# ( b is use here as a stack)
z
0 <M # until there is no more value
] sM # define macro M in [ and ]
lMx # execute macro M to populate stack b
[
Lb # get all values one at a time from stack b
p # print this value in binary
la # get the number of value
1
- # decremente it
d # duplicate
sa # keep one in register a
0<N # the other is use here
]sN # define macro N
lNx' # execute macro N to print each values in binary
是的,给你,champ solution++ve,太棒了:我也在路上写了这样的东西谢谢@RavinderSingh13。刚拿到我的金色awk标签。太好了,恭喜你。你真的活该,先生,向你致敬。是的,给你,champ solution++ve,太棒了:我也在路上写了这样的东西谢谢@RavinderSingh13。刚拿到我的金色awk标签。太好了,恭喜你。先生,向您致敬,这是您应得的。为什么我们需要费力地阅读大约50行数字,而不是仅仅4或5行来理解您的输入?您的问题提供了一个注释:简洁、可测试的示例输入和预期输出,以及您迄今为止尝试过的内容(如果您需要帮助)。看看这是否还不清楚。我不敢相信你能找到答案!为什么我们需要费力地阅读大约50行数字,而不是仅仅4或5行来理解您的输入?您的问题提供了一个注释:简洁、可测试的示例输入和预期输出,以及您迄今为止尝试过的内容(如果您需要帮助)。看看这是否还不清楚。我不敢相信你能找到答案!ctac!。。我看到你的很多答案都是使用dc和一些随机字符串生成器之类的选项:-,你是在某处查找答案还是练习过?。。我一直在想!。。总之+++@stack0114106我不确定我是否理解你说的话,就像G-Man认为所有人都可以很容易地给出解释:。关于dc的文档不多。每次我使用它,我都必须使用man dc。它几乎是汇编语言,所以它是二进制的。我在回答中为你添加了解释。。我看到你的很多答案都是使用dc和一些随机字符串生成器之类的选项:-,你是在某处查找答案还是练习过?。。我一直在想!。。总之+++@stack0114106我不确定我是否理解你说的话,就像G-Man认为所有人都可以很容易地给出解释:。关于dc的文档不多。每次我使用它,我都必须使用man dc。它几乎是汇编语言,所以它是二进制的。我在回答中为你添加了解释。