bitwise XOR of hex numbers in python

后端 未结 5 1467
陌清茗
陌清茗 2020-12-02 09:40

how can we XOR hex numbers in python eg. I want to xor \'ABCD\' to \'12EF\'. answer should be B922.

i used below code but it is returning garbage value



        
5条回答
  •  萌比男神i
    2020-12-02 09:43

    here's a better function

    def strxor(a, b):     # xor two strings of different lengths
        if len(a) > len(b):
            return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])
        else:
            return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])])
    

提交回复
热议问题