Hex to Binary conversion in bash

后端 未结 4 1097
说谎
说谎 2021-02-20 14:08

I\'m trying to convert a series of bytes from hex to bin using bash. but I keep getting (seemingly random) \"(standard_in) 1: syntax error\" replies from the following code:

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-20 14:42

    BC is a bit sensitive to case for hex values, change to uppercase and it should work

    for j in C4 97 91 8C 85 87 C4 90 8C 8D 9A 83 81
    do
            BIN=$(echo "obase=2; ibase=16; $j" | bc )
            echo $BIN
    done
    

    Output:

    11000100
    10010111
    10010001
    10001100
    10000101
    10000111
    11000100
    10010000
    10001100
    10001101
    10011010
    10000011
    10000001
    

提交回复
热议问题