def xorHexStrings(hexString1 : String, hexString2 : String) = { val iterator1 = hexString1.sliding(2, 2) val iterator2 = hexString2.sliding(2, 2) val result = new StringBuilder if (hexString2.length > hexString1.length) { while (iterator1.hasNext) { val i = Integer.toString(Integer.parseInt(iterator1.next, 16) ^ Integer.parseInt(iterator2.next, 16), 16) if (i.length == 1) result.append("0") result.append(i) } while (iterator2.hasNext) result.append(iterator2.next) } else { while (iterator2.hasNext) { val i = Integer.toString(Integer.parseInt(iterator1.next, 16) ^ Integer.parseInt(iterator2.next, 16), 16) if (i.length == 1) result.append("0") result.append(i) } while (iterator1.hasNext) result.append(iterator1.next) } result.toString() }
If we call this method within our main function, we can see the results.
def main(args : Array[String]) { val hexString1 = "1274560603" val hexString2 = "876429" //prints 95107f0603 println(xorHexStrings(hexString1, hexString2)) }
Can you explain how one would xor hex strings without programming? Thank you!
ReplyDeleteYou could either write the values out on paper and do the exclusive OR by hand, or you could use the calculator app on Windows using View -> Programmer (Alt+3). If neither of those methods work, let me know. I might need to know more about the problem you're up against.
Delete