Search code examples
arraysswift3xor

Bitwise XOR on two digit hex string in Swift 3


I want to get Bitwise XOR value from 1st two strings of array and then result of 1st two should be XORed with 3rd element of array and result of this should be XORed with 4th element of array and so on..

Array is ["a2", "09", "00", "00", "00", "01", "00", "00", "00", "10", "00"]

Final answer will also be bitWise XOR value

Please help.

Thanks in advance


Solution

  • You can use below code to get your result

    let numbers = pairs.flatMap{Int($0, radix: 16)}

    let xor = numbers.reduce(0, {$0^$1})

    let result = String(format: "%02x", xor)