Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Hex String XOR

by moritz (Cardinal)
on Mar 12, 2012 at 09:02 UTC ( #959088=note: print w/ replies, xml ) Need Help??


in reply to Hex String XOR

You need to pack the hex representation into a binary string, so if you have '112233' you need to create the bytes "\x11\x22\x33". Then you can use the bitwise operators on these byte strings to do what you want.

Here's a nice way to do the conversions:

use strict; use warnings; use 5.010; # just needed for say() sub unascii { pack 'h*', $_[0]; } sub ascii { unpack 'h*', $_[0]; } say ascii unascii('aabbcc') ^ unascii('112233'); # output: bb99ff

See pack, unpack and perlpacktut for more details.


Comment on Re: Hex String XOR
Download Code
Re^2: Hex String XOR
by ikegami (Pope) on Mar 12, 2012 at 18:45 UTC

    "unascii" and "ascii" aren't very clear to me. I would call them "hex_to_bytes" and "bytes_to_hex" respectively.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://959088]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (23)
As of 2014-07-30 16:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (236 votes), past polls