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 words function might help.
For 2, read "0xBD" == 189. You just need to work out how to get each hexadecimal number into the right format for read. (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) (where i is the one converted from hexadecimal, and y is the index in the list, and x is the value of the character) and solve for x. And chr is the inverse to ord.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With