Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Hex String XOR

by moritz (Cardinal)
on Mar 12, 2012 at 09:02 UTC ( [id://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.

Replies are listed 'Best First'.
Re^2: Hex String XOR
by ikegami (Patriarch) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://959088]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-26 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found