Solved

Bitwise XOR of all characters in string

Posted on 2006-10-25
6
1,277 Views
Last Modified: 2012-06-22
I'm trying to read NMEA gps signal from COM1, modifying it and sending it out on COM2. In this case I need to check the chekcsum of the incoming signal, and generate a checksum for the new modified data stream.

The incoming data is like this
$abc,def,ghi,jkl*FF
I need to do this
a XOR b XOR c XOR , XOR d  (...) XOR k XOR l

i.e. XOR all characters between $ and *
Then the checksum is the hexadesimal value typed at the end of the string e.g. FF

What is the fastest and most efficient way of doing this?   I don't mind putting ASM code in the project if that will be the most efficient and elegant.

Arild
0
Comment
Question by:arildj78
  • 3
  • 2
6 Comments
 
LVL 28

Accepted Solution

by:
pepr earned 500 total points
ID: 17806135
This is probably only part of the answer. Specify better your question

#include <string>
#include <iostream>

int main()
{
    using namespace std;
    string s("$abc,def,ghi,jkl*");

    int sum = 0;

    for (string::size_type i = 1; i < s.length() - 1; ++i)
    {
        sum ^= s[i];  // equal to sum = sum ^ s[i];  i.e. XOR
    }

    cout << "'" << s << hex << sum << "'" << endl;

    return 0;
}
0
 
LVL 28

Expert Comment

by:pepr
ID: 17806151
This kind of checksum is not reliable. Try CRC instead. Have a look at http://cpp.snippets.org/code/crc.txt -- A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS by Ross N. Williams.

The sources can be found at http://cpp.snippets.org/code/
as crc.cpp, crc.hpp, crc.h, crc_16.c, crc_16f.c, crc_32.c (depends what you need and like).
0
 
LVL 2

Author Comment

by:arildj78
ID: 17806431
I'm all new to C++ but this seems to solve my XOR problem as stated in my question. The rest of the program will probably show up here as questions ;)

I've read a bit about checksums and am aware of the weaknesses of this way of doing it, but this is the way that is speciefied in the NMEA-0183 protocol commonly used in GPS to RS-232 communications.

I will post again as soon as I've tested the code.


Can you explain how the code works?
                                              for (string::size_type i = 1; i < s.length() - 1; ++i)
                                              {
                                                  sum ^= s[i];  // equal to sum = sum ^ s[i];  i.e. XOR
                                              }

                                              cout << "'" << s << hex << sum << "'" << endl;

                                              return 0;
I'm not to sure how this works :(   If you don't have time, that's ok.  I will probably find out anyway  :)

Arild
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 8

Expert Comment

by:Exceter
ID: 17807263
>> Can you explain how the code works?

>> for (string::size_type i = 1; i < s.length() - 1; ++i)
Initiates a for loop with a counter names i of type string::size_type. i is incremented once for every loop iteration. The loop terminates when i is greater than or equal to the length of the string.

>> sum ^= s[i];  // equal to sum = sum ^ s[i];  i.e. XOR
The ^= operator takes the left operand, which is the integer variable sum, XORs it against the right operand, which is the character s[i], and stores the result in the integer variable sum.

>> cout << "'" << s << hex << sum << "'" << endl;
Displays the string s followed by the integer sum encapsulated by single quote marks.

>> return 0;
Returns 0 and ends function execution. As this is in the main function, this also has the effect of ending program termination.

Cheers!
Exceter
0
 
LVL 8

Expert Comment

by:Exceter
ID: 17807272
Sorry for the double negative... I meant to say, "ending program execution."
0
 
LVL 28

Expert Comment

by:pepr
ID: 17855966
Arildj78: Well, I know nothing about the NMEA-0183 protocol. It is likely that the XOR is used because it is an extremely easy operation for even the simplest processor. There probably is no faster operation. The program that I wrote mainly demonstrates the XOR operation itself. Also, the string is processed from the second character (zero based indexing and index 1) to skip the $ and it does not include the last character '*'. It also does not assume here that you could receive the sum on the input to check it. The result is only displayed, not converted and appended to the string.

The most magical could be the
cout << "'" << s << hex << sum << "'" << endl;

Think about it as about the print command that displays the string and the sum.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question