Group,
I have some doubts in the following program.
------------------program---------------------
/*
** Make an ascii binary string into an integer.
*/
#include <string.h>
unsigned int bstr_i(char *cptr)
{
unsigned int i, j = 0;
while (cptr && *cptr && strchr("01", *cptr))
{
i = *cptr++ - '0';
j <<= 1;
j |= (i & 0x01);
}
return(j);
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *arg;
unsigned int x;
while (--argc)
{
x = bstr_i(arg = *++argv);
printf("Binary %s = %d = %04Xh\n", arg, x, x);
}
return EXIT_SUCCESS;
}
-----------------------------------------------------------
My doubts are:
1)In the expression
i = *cptr++ - '0';
the pointer cptr is dereffered and subtracted with the char constant
'0'. As far as I know, the above expression will return the number of chars
in-between cptr and '0'. Then the variable i will either 1 or 0.
Am I correct or wrong?
2)What is the meaning of "Make an ASCII binary string into an integer."
Any web reference or a link for the above.
3)What ever number I input I get the answer 0000h except when I input 1
by that I will get 0001h. Why is that?
In short I am very much confused by the above program. Can any one take
some time in explaining the while block in the function bstr_i().
--
"combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan AT gmail DOT com