I have to make a program that takes the char input from a file, generates their hex value and adds their hex values together (8 bit checksum).
Input:
while(fscanf(ifp, "%c", buffer) != EOF)
{
input[i] = *buffer;
printf("%02x", input[i]);
i++;
}
For example, if the input is "a", the initialoutput should be : 610a (0a for new line char) After adding those together you get 6b. And with "aa" the initial output should be : 61610a and "cc" as result of the addition.
So far I have made a program that reads in char-by-char and stores it. Then I made a recursive function to add the characters together
char addChar(char *input, int i, int size ) {
if( i == size ) {
return 0;
}
return input[i] + addChar(input, i+1, size);
}
and then I print.
printf("%02x\n", addChar(input, i, size)));
But when I run the program i keep getting a bunch of f's in front of some of the output. I know that is the overflow from adding them together but how do I get rid of that.
Input: a, aa, aaa, aaaa, aaaaa
[Terminal Output][1]
MacbookPro:CheckSum $ ./a.out i1.txt 8 610a 6b MacbookPro:CheckSum $ ./a.out i2.txt 8 61610a ffffffcc MacbookPro:CheckSum $ ./a.out i3.txt 8 6161610a 2d MacbookPro:CheckSum $ ./a.out i4.txt 8 616161610a ffffff8e MacbookPro:CheckSum $ ./a.out i5.txt 8 61616161610a ffffffef
unsigned char addChar(char *input, int i, int size )main()function