2

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
4
  • Show a minimal, complete example program. Commented Nov 4, 2018 at 20:41
  • use unsigned char addChar(char *input, int i, int size ) Commented Nov 4, 2018 at 20:59
  • post complete code include main() function Commented Nov 4, 2018 at 21:00
  • Changing it to unsigned char fixed the issue. Commented Nov 4, 2018 at 22:36

1 Answer 1

1

Try this:

#include <stdio.h>
#include <string.h>

unsigned char addChar( const unsigned char * input, size_t i, size_t size ) {
   if( i == size ) {
      return 0;
   }
   return input[i] + addChar( input, i+1, size );
}

int main( int argc, char * argv[] ) {
   {
      unsigned char input[] = { 'a', '\n' };
      printf( "a    : %02x\n", addChar( input, 0, sizeof( input )));
   }
   {
      unsigned char input[] = { 'a', 'a', '\n' };
      printf( "aa   : %02x\n", addChar( input, 0, sizeof( input )));
   }
   {
      unsigned char input[] = { 'a', 'a', 'a', '\n' };
      printf( "aaa  : %02x\n", addChar( input, 0, sizeof( input )));
   }
   {
      unsigned char input[] = { 'a', 'a', 'a', 'a', '\n' };
      printf( "aaaa : %02x\n", addChar( input, 0, sizeof( input )));
   }
   {
      unsigned char input[] = { 'a', 'a', 'a', 'a', 'a', '\n' };
      printf( "aaaaa: %02x\n", addChar( input, 0, sizeof( input )));
   }
   (void)argc;
   (void)argv;
   return 0;
}

The output:

a    : 6b
aa   : cc
aaa  : 2d
aaaa : 8e
aaaaa: ef
Sign up to request clarification or add additional context in comments.

2 Comments

This definitely helps me understand little bit more but the im reading in from a txt file from the cmd line so it will not always be a , aa, aaa, etc. So would I just remove the line before the print statement and continue as normal? I am updating the original post to show where im reading in the input.
Actually this worked perfectly. the only thing I had to change to my original code was the function. I had it returning a char instead of unsigned char. as well I was assigning the input to char instead of unsigned char. thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.