I’m trying to convert a 32 character long hex string (eg: A41EB86E184207262C12F66E7C480577) to binary. I’m using:
echo "ibase=16; obase=2; $line" | bc
but its including an unnecessary \ in-between which is not letting me perform any bit operations on the result.
An example output that I got is:
10100100000111101011100001101110000110000100001000000111001001100010\
110000010010111101100110111001111100010010000000010101110111
Notice the \ in the end of the first line.
What can I do to avoid it?
If you have a modern enough
bc, you can setBC_LINE_LENGTHto zero to disable wrapping:This output your number on a single line.
I tend to have that in my
.profile(or.bashrc, I can’t remember off the top of my head) since I like that to be the default:From the manpage:
If your
bcdoesn’t support the “0 means disable” extension, you can also set it ridiculously high:If your
bcis “challenged” in that it doesn’t allowBC_LINE_LENGTHat all, you may need to revert to post-processing of the output, such as usingtrto get rid of newlines and backslashes: