How to make `bc` output a desired number of base-2 binary digits
Asked Answered
C

3

1

This outputs 101110

echo "obase=2; 46" | bc

How can I make it output 8 digits, like this? : 00101110

I learned the above usage of bc here: Bash shell Decimal to Binary base 2 conversion

See also my answer to that question here.

Conscript answered 22/3, 2022 at 7:16 Comment(0)
T
3

The simple solution is to use the output of bc within a command substitution providing input to printf using the "%08d" conversion specifier, e.g.

$ printf "%08d\n" $(echo "obase=2; 46" | bc)
00101110
Twopiece answered 22/3, 2022 at 8:1 Comment(7)
Oh my goodness! I can't believe I didn't think of that! That's so much easier! It didn't occur to me because 101110 is not a decimal (%d) integer number (and in C, which my mind focuses on more, this would be a string, which really isn't a decimal number!). Yet...technically, in bash at least, the string 101110 can be / is a decimal number, and printf in bash is ok treating it like one, so this works.Conscript
Also, as a lawyer, how do you have so much time to write code, and how did you get so skilled at it? Is it a hobby you do at work?Conscript
Sometimes it's just the luck of the draw. I made friends with printf a long time ago in C, so it's kinda the go-to for formatting wanted integer (or binary) output in this case. Since we are talking less than 8 digits, you can treat the binary output with a leading 1 as an integer :)Twopiece
Everybody needs a diversion. Since I was an engineer before becoming a lawyer (25 years ago), it's nice to actually deal in a topic that is black-and-white now and again.Twopiece
By the way, here is my printf version which I had come up with here: printf "0b%s\n" "$(echo "obase=2; $((num1 + num2))" | bc)". I was thinking of the output of bc as a string, not a decimal integer.Conscript
That works too. The reason I prefer the integer conversion specifier is that it allows for both a zero-padding and field-width modifier. Treated as a string, you are limited to the field-width modifier. You can still add the fixed "0b" prefix if you like. Good luck with your scripting!Twopiece
Another option is to use your string-specifier with an 8-character field width and then do a global replacement of spaces to 0, e.g. str=" 101110"; str="${str// /0}"Twopiece
T
2

Some other solutions:

echo "obase=2; 46" | bc | awk '{ printf("%08d\n", $0) }'
echo "obase=2; 46" | bc | numfmt --format=%08f
Tiberius answered 22/3, 2022 at 10:9 Comment(0)
C
0

If there's an easier way I'd like to know, but here's a function I just wrote that does it manually:

decimal_to_binary_string.sh: (from my eRCaGuy_hello_world repo)

# Convert a decimal number to a binary number with a minimum specified number of
# binary digits
# Usage:
#       decimal_to_binary <number_in> [min_num_binary_digits]
decimal_to_binary() {
    num_in="$1"
    min_digits="$2"
    if [ -z "$min_chars" ]; then
        min_digits=8  # set a default
    fi

    binary_str="$(echo "obase=2; 46" | bc)"

    num_chars="${#binary_str}"
    # echo "num_chars = $num_chars" # debugging
    num_zeros_to_add_as_prefix=$((min_digits - num_chars))
    # echo "num_zeros_to_add_as_prefix = $num_zeros_to_add_as_prefix" # debugging
    zeros=""
    for (( i=0; i<"$num_zeros_to_add_as_prefix"; i++ )); do
        zeros="${zeros}0"  # append another zero
    done

    binary_str="${zeros}${binary_str}"
    echo "$binary_str"
}

# Example usage
num=46
num_in_binary="$(decimal_to_binary "$num" 8)"
echo "$num_in_binary"

Output:

00101110
Conscript answered 22/3, 2022 at 8:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.