1

I want to calculate the checksum of my data. After converting the string to binary i now need to sum all of the 8 bit strings (XOR them) such that, if the XOR produces an overflow bit (carry bit) at the end of the sum, that bit should be added to the final sum and the value obtained now is the FINAL value (an 8 bit checksum).

Then i want to take a 1s complement of the 8 bit FINAL value and this new value would be my actual checksum that i can use ahead. I dont know how to take up each of these binary strings and sum them in the first place :(

#include <string>
#include <bitset>
#include <iostream>
using namespace std;
int main()
{
    string myString = "Hello World";
    for (std::size_t i = 0; i < myString.size(); ++i)
{
       cout << bitset<8>(myString.c_str()[i]) << endl;
}


//the indentations might have shaken a bit in copting the code here. 

}

user899714
  • 455
  • 6
  • 13
  • 23
  • 5
    XOR can't overflow. Are you sure you know what XOR is and how it differs from summation? – user2357112 Nov 05 '13 at 21:30
  • This sounds like a partial description of a CRC, but as stated it doesn't begin to make sense. – user207421 Nov 05 '13 at 22:12
  • Yes it is partial bcoz i dont want a CRC check just a simple checksum..it would be complete once i get the sum of all the 8 bit sets displayed by the ouput and then 1s complement it. I want a binary summation (1 + 1 = 1 0 here...the 1 from 1 0 is the carry bit til it reaches MSB, once it does,it becomes an overflow bit (iv termed it overflow bit) and there it gets added to the sum (the sum value excluding this last carry/overflow bit) \to get the final sum value ). – user899714 Nov 06 '13 at 16:05

1 Answers1

0

You can code a function like this if I well understood the problem :

unsigned char checkSum(const string &str) {
    unsigned char answer = 0;
    for (size_t i = 0; i < myString.size(); ++i)
    {   // XOR each byte of the string and stores the result in answer.
        answer ^= str[i];
    }

    return answer;
}

Usage :

unsigned char my_checksum = checkSum("Hello World");

Notice that if you XOR unsigned char (0 to 255) together, you'll always get a result between 0 and 255. The function XORes each byte of the string you passed as input parameter together and return the result. This algorithm is also called LRC (Longitudinal Redundancy Check).

Hope this is want you wanted.

Gabriel L.
  • 4,678
  • 5
  • 25
  • 34
  • why are you summing a 1 in return ((answer ^ 0xFF) + 1) & 0xFF ? – user899714 Nov 06 '13 at 16:39
  • Thanks! :) if i want to do the same for 16 bits (a pair of bytes) how should i edit the above code? should i use bitset or something? secondly, is the lRC summing all 'sets' of 8 bit streams to each other? with the edited version, i have a feeling that it is summing the entire stream bit wise to each other. or maybe i am not clear on it yet. – user899714 Nov 07 '13 at 07:45
  • For 16 bits, it's called 16-bit crc-ccitt. I suggest you to read [this answer](http://stackoverflow.com/questions/15169387/definitive-crc-for-c). There are useful links about implementation of CRC. – Gabriel L. Nov 07 '13 at 12:48