How to perform the xor operation in a char?
-
Hey guys, I have a problem! I am trying to do a cryptography algorithm and I need to perform a xor operation between two Qstrings char by char and I have no idea of how to do it.
-
Don't confuse char (data type, usually of size 8 bits) with QString character(one or more QChar of size 16 bits) as these are different concepts.
Cryptography usually doesn't deal with characters, just bytes, so
I'll assume you really want to iterate over chars (the data type), not characters. That being said the result of such xor might not be a valid QString (xor might produce invalid unicode data for some characters).What you need to do is use one of QString methods to convert the QString to QByteArray and work with that. Note that the resulting xored QByteArray may not be representable as a valid QString eg. if you use toLatin1() the result of xor will be a valid Latin1 string, but with toUtf8() the result of xor might become invalid unicode data, so don't try to convert the xored state back to QString.
To access each char of QByteArray you can use its at() method or the [] operator and for xoring there's the language built-in ^ operator.
-
thanks Chris it was very helpfull