Need a fast oneline decimal-to-binary conversion without loop in bash.

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
blumenwesen
Posts: 32
Joined: Sun Apr 10, 2022 10:02 pm

Need a fast oneline decimal-to-binary conversion without loop in bash.

Post by blumenwesen »

Does anyone know a faster calculation path for decimal to binary than in a loop?
I think the principle is similar like from decimal to octal, only the calculation without a loop would be longer.

Code: Select all

#dec-bin ???
z="338"
echo -e $["$z"/32]$[$["$z"/2]%2]$["$z"%2] # failed attempt

#dec-bin
z="338"
y=""
while [ $z -ne 0 ]; do
    y=$[$z%2]$y
    z=$[$z/2]
done
echo $y

#dec-oct
z="338"
echo -e $["$z"/64]$[$["$z"/8]%8]$["$z"%8]

#dec-oct
z="338"
y=""
while [ $z -ne 0 ]; do
    y=$[$z%8]$y
    z=$[$z/8]
done
echo $y
williams2
Posts: 1026
Joined: Sat Jul 25, 2020 5:45 pm
Been thanked: 291 times

Re: Need a fast oneline decimal-to-binary conversion without loop in bash.

Post by williams2 »

bash is really not good for arithmetic, usually something like bc is used. For example,

Code: Select all

# echo 'obase=2;338' | bc
101010010
# 
## https://github.com/louisrubet/rpn
# rpn 338 bin
0b101010010
# 
# ff 338 2 base ! . ;
\ Loading'ff.ff: help normal .now {{{ +longconds f. sqrt uart! hid'm
\ FreeForth 1.2 <http://christophe.lavarenne.free.fr/ff>  Try: help
 0; 101010010 
#
# python -c 'print("{:b}".format(338))'
101010010
#

Do you really need it coded in bash?

blumenwesen
Posts: 32
Joined: Sun Apr 10, 2022 10:02 pm

Re: Need a fast oneline decimal-to-binary conversion without loop in bash.

Post by blumenwesen »

Thanks but I didnt want to take python anymore because they even changed basic databases so that other programs and libraries have more direct access.
Since 2.7 is no longer improved, many libraries no longer work properly.
I thought there was a way to calculate only with echo, because I found out that it works everywhere, only with binary I don't have an example yet.

Code: Select all

#hex-oct
z="0152"
echo -e $[$[16#"$z"]/64]$[$[$[16#"$z"]/8]%8]$[$[16#"$z"]%8]
#oct-hex
z="522"
echo -e $[$[8#"$z"]/256]$[$[$[8#"$z"]/16]%16]$[$[8#"$z"]%16]
#dec-hex
z="338"
echo -e $["$z"/256]$[$["$z"/16]%16]$["$z"%16]
#bin-hex
z="101010010"
echo -e $[$[2#"$z"]/256]$[$[$[2#"$z"]/16]%16]$[$[2#"$z"]%16]
# and so on ...
blumenwesen
Posts: 32
Joined: Sun Apr 10, 2022 10:02 pm

Re: Need a fast oneline decimal-to-binary conversion without loop in bash.

Post by blumenwesen »

I found the correct calculation only a little more cumbersome than thought and the hex conversion is incorrect.

Code: Select all

# z="255"
z="338"
z=$(echo -e $["$z"/256]$[$["$z"/16]%16]$["$z"%16])
# z=$(echo -e $["$z"/256]$[$["$z"/16]%16]$["$z"%16] | awk '{gsub("10", "a"); gsub("11", "b"); gsub("12", "c"); gsub("13", "d"); gsub("14", "e"); gsub("15", "f")}1')
echo -e $[($[16#$z]>>10)%2]$[($[16#$z]>>9)%2]$[($[16#$z]>>8)%2]$[($[16#$z]>>7)%2]$[($[16#$z]>>6)%2]$[($[16#$z]>>5)%2]$[($[16#$z]>>4)%2]$[($[16#$z]>>3)%2]$[($[16#$z]>>2)%2]$[($[16#$z]>>1)%2]$[($[16#$z]>>0)%2]
Post Reply

Return to “Programming”