hi Forum, how can i convert items in a Array from typ Char[] in an Integer-Type? example: char data[] = "110111"; int sum = 0; sum = (int)data; or should i sum the all items in the array bitwise?
I'm assuming you are converting a binary number stored as a string into int. This should do the job:
char data[] = "110111"; int sum = 0; int i; for (i=0; data[i] != '\0'; i++) { sum <<= 1; if ( data[i] == '1' ) sum |= 1; } /* it's done */
char data[] = "110111"; int sum = 0; int i; for (i=0; data[i] != '\0'; i++) { sum += data - '0'; } /* it's done */
You seem to have a fundamental misunderstanding about what an array is. In 'C', an array's name gives the address of the 1st element of the array - it is effectively a pointer to the start of the array.
sum = (int)data;
sum = array[0] + array[1] + array[2] etc, etc,...
char array[] = "110111";