Search code examples
shellawkcshbc

bc does not completely convert long hexadecimal numbers


I am using bc to convert a long hex vectors to binary. It does not work for the following code example in my awk script:

#!/bin/awk -f

cmd = "bc <<< \"ibase=16;obase=2;"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\""

result = ((cmd | getline binary_vector) > 0 ? binary_vector : -1)
close(cmd)
print "conversion result: " result

I get the following as output:

conversion result: 11111111111111111111111111111111111111111111111111111111111111111111\

This is not the complete result. bc does not convert the whole string and truncates in between with a "\". How can I convert the whole hexadecimal number?


Solution

  • The following command makes bc return a binary string of 1000 characters in one line.

    cmd = "BC_LINE_LENGTH=1000 bc <<< \"ibas ...\"