I have the following code in haskell that i want to make some changes to it:
unwords . map (printf "%02X") $ zipWith (\x y -> -(fromIntegral (ord x)) + y - 2 :: Word8) "Aa123456" [0..]
After running this code i get:
"BD 9E CF CF CF CF CF CF"
Basically i want the inverse of this function, so the function can get the hex values "BD 9E CF CF CF CF CF CF" and return "Aa123456".
I am sure i would have to change the printf statement but how i am suppose to change the script to accept the hex values ?
As @gspr suggests, the way to solve this is to break it into smaller parts:
"BD 9E CF CF CF CF CF CF"into["BD", "9E", ...., "CF", "CF"].For 1, the
wordsfunction might help.For 2,
read "0xBD" == 189. You just need to work out how to get each hexadecimal number into the right format forread. (Note, that you could also write your conversion function to go directly from string to integer, it might be a nice little bit of practice.)For 3, just invert the encoding operation, i.e.: write your equation
i = y - x - 2 (mod 256)(whereiis the one converted from hexadecimal, andyis the index in the list, andxis the value of the character) and solve forx. Andchris the inverse toord.