Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. xor strings??

xor strings??

Scheduled Pinned Locked Moved C#
csharptutorialquestion
8 Posts 4 Posters 0 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.
  • K Offline
    K Offline
    Karavaev Denis
    wrote on last edited by
    #1

    How to make simple xor in C#? ===================== http://wasp.elcat.kg

    K D 2 Replies Last reply
    0
    • K Karavaev Denis

      How to make simple xor in C#? ===================== http://wasp.elcat.kg

      K Offline
      K Offline
      Kannan Kalyanaraman
      wrote on last edited by
      #2

      Hi, If you are looking at nos. then you can use ^ operator. For strings, you need to first convert them to bytes, ASCIIEncoding enc = new ASCIIEncoding (); byte[] keybytes = enc.GetBytes (key); here key is of type string once you have converted to bytes, you can work on them. Hope this helps. Cheers Kannan

      K 1 Reply Last reply
      0
      • K Karavaev Denis

        How to make simple xor in C#? ===================== http://wasp.elcat.kg

        D Offline
        D Offline
        Daniel Turini
        wrote on last edited by
        #3

        Besides the above suggestion, you can after this, create a BitArray with the byte[] and use its Xor method. I see dumb people

        1 Reply Last reply
        0
        • K Kannan Kalyanaraman

          Hi, If you are looking at nos. then you can use ^ operator. For strings, you need to first convert them to bytes, ASCIIEncoding enc = new ASCIIEncoding (); byte[] keybytes = enc.GetBytes (key); here key is of type string once you have converted to bytes, you can work on them. Hope this helps. Cheers Kannan

          K Offline
          K Offline
          Karavaev Denis
          wrote on last edited by
          #4

          string txt1 = "some_text"; string txt2 = "anything_else"; ASCIIEncoding enc = new ASCIIEncoding(); ASCIIEncoding enc2 = new ASCIIEncoding(); byte[] kb1 = enc.GetBytes(txt1); byte[] kb2 = enc2.GetBytes(txt2); MessageBox.Show(kb1 ^ kb2); Operator '^' cannot be applied to operands of type 'byte[]' and 'byte[]' damm, what I did wrong? ===================== http://wasp.elcat.kg

          C K 2 Replies Last reply
          0
          • K Karavaev Denis

            string txt1 = "some_text"; string txt2 = "anything_else"; ASCIIEncoding enc = new ASCIIEncoding(); ASCIIEncoding enc2 = new ASCIIEncoding(); byte[] kb1 = enc.GetBytes(txt1); byte[] kb2 = enc2.GetBytes(txt2); MessageBox.Show(kb1 ^ kb2); Operator '^' cannot be applied to operands of type 'byte[]' and 'byte[]' damm, what I did wrong? ===================== http://wasp.elcat.kg

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            You need to ^ each byte, not the whole lot. You need to step through the strings, and can only perform the operation on the set of bytes that exists in both arrays, so unless the two strings are always the same length ( which they are not in your example ), I doubt you're going to get what you wanted. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
            C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
            Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002

            K 1 Reply Last reply
            0
            • K Karavaev Denis

              string txt1 = "some_text"; string txt2 = "anything_else"; ASCIIEncoding enc = new ASCIIEncoding(); ASCIIEncoding enc2 = new ASCIIEncoding(); byte[] kb1 = enc.GetBytes(txt1); byte[] kb2 = enc2.GetBytes(txt2); MessageBox.Show(kb1 ^ kb2); Operator '^' cannot be applied to operands of type 'byte[]' and 'byte[]' damm, what I did wrong? ===================== http://wasp.elcat.kg

              K Offline
              K Offline
              Kannan Kalyanaraman
              wrote on last edited by
              #6

              You will have to iterate the array and do the operations individually on every byte. for(int i=0; i<10; i++) kb1[i] ^= kb2[i]; Cheers Kannan

              1 Reply Last reply
              0
              • C Christian Graus

                You need to ^ each byte, not the whole lot. You need to step through the strings, and can only perform the operation on the set of bytes that exists in both arrays, so unless the two strings are always the same length ( which they are not in your example ), I doubt you're going to get what you wanted. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
                C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
                Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002

                K Offline
                K Offline
                Karavaev Denis
                wrote on last edited by
                #7

                Ok, is there any other simple way to encode\decode some string? ===================== http://wasp.elcat.kg

                C 1 Reply Last reply
                0
                • K Karavaev Denis

                  Ok, is there any other simple way to encode\decode some string? ===================== http://wasp.elcat.kg

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  Yes, there is apparently heaps of encryption/decryption stuff in .NET, but I confess to not having used any of it, yet. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
                  C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
                  Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups