Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to perform the xor operation in a char?
Forum Updated to NodeBB v4.3 + New Features

How to perform the xor operation in a char?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    suetamoviki
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        suetamoviki
        wrote on last edited by
        #3

        thanks Chris it was very helpfull

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved