python中字符串异或_如何在Python中异或两个包含十六进制数字的字符串?

I have looked for an answer online, but none of them seem to solve my problem in my way (I know, I'm picky :D).

Here's the deal: I am using the string type to store two hex numbers, because the default integer type in python is not long enough for my purposes. For example like this:

S1 = "315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc41556bdb36bbca3e8774301fbcaa3b83b220809560987815f65286764703de0f3d524400a19b159610b11ef3e"

S2 = "234c02ecbbfbafa3ed18510abd11fa724fcda2018a1a8342cf064bbde548b12b07df44ba7191d9606ef4081ffde5ad46a5069d9f7f543bedb9c861bf29c7e205132eda9382b0bc2c5c4b45f919cf3a9f1cb74151f6d551f4480c82b2cb24cc5b028aa76eb7b4ab24171ab3cdadb8356f"

The point is, that these are supposed to be NUMBERS, but they are stored in a string. What I want to do, is to treat these two strings as numbers, perform a bitwise XOR on the two and then get an output in a similar form - a hex number stored in a string.

I am rather new to programming and even newer to python, so I couldn't figure out a way to do this. I am not just looking for a script, I would also like to understand how it works, so please be thorough with your explanation, as I am quite the noob :D.

解决方案

Python can hold your values as numbers.

See this for proof,

>>> hex(int(S1, 16))[2:-1] == S1

True

I'm simply adjusting the string, removing '0x' from the beginning and L from the end.

For your answer all you need to do is,

hex(int(S1, 16) ^ int(S2, 16))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值