Friday 15 January 2010

c - Checking each char in a string giving unexpected results -



c - Checking each char in a string giving unexpected results -

i'm making base64 encoder/decoder, have function encode input binary base64 representation. input string of 0's , 1's.

my if statement failing validate each char in string '0' or '1'. says every char not '0' or '1'. though is.

my code: (not total functions code...)

#include <stdio.h> #include <string.h> char *enc(char *); int main(void) { enc("1101010100100101001010010101001"); homecoming 0; } char *enc(const char *data) { int i; for(i = 0; < strlen(data); i++) { if(data[i] != '0' || data[i] != '1') { printf("index %d not 0 or 1\n", i); printf("instead is: %c\n", data[i]); } } homecoming null; }

this outputs every char not 0 or 1.

ideone: http://ideone.com/0mqnn4

if(data[i] != '0' || data[i] != '1')

if char not 0 or char not 1 -> true. think meant and:

if(data[i] != '0' && data[i] != '1')

"not or b" in english language translates "not (a or b)" in boolean logic, de morgan's laws, equivalent "(not a) , (not b)".

c string char base64

No comments:

Post a Comment