i am trying to convert a string containing a 16-bit number in binary to a integer value. It is a homework assignment and i have to use scanf("%1d.... ). The problem i am having is that the loop wont end, i have no clue how to fix it.
for example: input: 0000000000001111
output: 15
int read_binary_value() { int value = 0; while( scanf("%1d", &value) == 1) { printf("%d ", value); if (value == 1) { value += 1; value << 1; } } printf("yoyoyoyoyoyoyo"); printf("%d",value); return value; }
forloop with 16 iterations instead of the currentwhileloop. Then the loop completes when all 16 digits have been read or an invalid character is encountered.strtol()would be preferable.