【发布时间】:2022-10-17 05:41:47
【问题描述】:
问候 我目前正在开发一个函数,它将每个十进制转换为二进制而不使用 awk sed printf xxd od perl ibase, obase, bc 但是,该函数设法将十进制转换为二进制,但由于某种原因,它在转换后的二进制末尾输出“expr:除以零”
我试图删除 expr 并将其设置为普通公式,但它分发了另一个错误,所以我别无选择,因为它是将十进制转换为二进制的壁橱的东西
for i in $d do #$d is the decimal
num = $d #decimal number
div = 128 #it is the power number (we should start dividing by 128)
sec = 0 #to run the loop 8 times
while [[ $seq -ne 9 ]]
do
bin=`expr $num / $div`
echo -n "$bin" # we can add the replacing x and space here
rem=`expr $num % $div` # gets the remainder
div=$(expr $div / 2) #to get the decreasing power of 2
num=$rem #next the num should be equal to the remainder
sec=$(sec + 1)
done
done
#OUTPUT
Output : 11111000expr:division by zero
任何提示将不胜感激
【问题讨论】:
-
通过将
sec设置为 0 并循环直到sec为 9,即循环 9 次,而不是 8 次。 -
@sj95126 感谢您的注意,我把它降到了 8,不幸的是,输出聚集在一起,而不是将每个转换后的二进制文件分成自己的列。