awk实现的从2进制到10进制,10进制到2进制的转换功能
#!/usr/bin/awk -f
function dec2bin(num)
{
s="";
while(num!=0)
{
s=num%2s;
num=(num-num%2)/2;
}
s="00000000"s;
return substr(s,length(s)-7);
}
function bin2dec(num)
{
mod=1;
val=0;
for(i=length(num); i>0; i--)
{
val+=substr(num,i,1)*mod;
mod*=2;
}
return val;
}
{
getline a;
bin=dec2bin(a);
dec=bin2dec(bin);
print bin,dec;
exit 0;
}
阅读(1387) | 评论(0) | 转发(0) |