bc and its ibase/obase options:

前端 未结 3 1179
眼角桃花
眼角桃花 2021-02-01 10:58

I stumbled over a curious bug, I think:

I tried to read \"512\" as a number to base 6, and output it as base 16:

echo \"ibase=6;obase=16;512\" | bc
161         


        
3条回答
  •  被撕碎了的回忆
    2021-02-01 11:40

    Slightly modify the code (two ways) and your expected results appear:

    for ib in {6,8,10,16}; do 
        echo $ib; for ob in {10,16}; do 
            echo -en $ib $ob"     \t => " ; 
            ob=`echo "obase=$ib;$ob" | bc`
            echo "ibase=$ib;obase=$ob;333" | bc ; 
        done; 
    done;
    
    for ib in {6,8,10,16}; do 
        echo $ib; for ob in {10,16}; do 
            echo -en $ib $ob"     \t => " ; 
            echo "obase=$ob;ibase=$ib;333" | bc ; 
        done; 
    done;
    

    execute in bash sandbox

    results for both ways are:

    6
    6 10         => 129
    6 16         => 81
    8
    8 10         => 219
    8 16         => DB
    10
    10 10        => 333
    10 16        => 14D
    16
    16 10        => 819
    16 16        => 333
    

提交回复
热议问题