Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9220933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:27:49+00:00 2026-06-18T03:27:49+00:00

Possible Duplicate: bitwise XOR of hex numbers in python I am trying to XOR

  • 0

Possible Duplicate:
bitwise XOR of hex numbers in python

I am trying to XOR two hex strings in Python and did not really know where to start from.

I have two hex strings:

a = "32510ba9a7b2bba9b8005d43a304b5714cc0bb0c8a34884dd91304b8ad40b62b07df44ba6e9d8a2368e51d04e0e7b207b70b9b8261112bacb6c866a232dfe257527dc29398f5f3251a0d47e503c66e935de81230b59b7afb5f41afa8d661cb"
b = "32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e9052ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f90"

Should I be using this ?

  1. return "".join([chr((x) ^ (y)) for (x,y) in zip(a[:len(b)], b)])
  2. return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])

I don’t understand the difference with the two codes above. Why chr and ord? I have also seen people using int(hex,16).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:27:50+00:00Added an answer on June 18, 2026 at 3:27 am

    You are missing a couple of things here.

    First, you will not want to XOR those strings. You have the strings in an encoded form, therefore, you need to .decode() them first:

    binary_a = a.decode("hex")
    binary_b = b.decode("hex")
    

    Then, as already mentioned, the zip() function stops iterating as soon as one of the two sequences is exhausted. No slicing is needed.

    You need the second version of the loop: First, you want to get the ASCII value of the characters: ord() produces a number. This is necessary because ^ only works on numbers.

    After XORing the numbers, you then convert the number back into a character with chr:

    def xor_strings(xs, ys):
        return "".join(chr(ord(x) ^ ord(y)) for x, y in zip(xs, ys))
    
    xored = xor_strings(binary_a, binary_b).encode("hex")
    

    Using .encode() at the end, we get the binary string back into a form, that prints nicely.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What USEFUL bitwise operator code tricks should a developer know about? Hi,
Possible Duplicate: how to perform bitwise operation on floating point numbers Hello, everyone! Background:
Possible Duplicate: Trying to get tables next to each other horizontal I have two
Possible Duplicate: Using a bitwise & inside an if statement I have the following
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: Why does my cout output not appear immediately? I have a very
Possible Duplicate: Learning to write a compiler I looked around trying to find out
Possible Duplicate: Reading through file using ifstream I'm trying to find a way to
Possible Duplicate: Bitwise and in place of modulus operator Can someone explain the rationale
Possible Duplicate: Model.find(1) gives ActiveRecord error when id 1 does not exist If there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.