Syntactic Confectionery Delight PerlMonks

### Perl XOR

by fixu2 (Initiate)
 on Sep 18, 2013 at 18:04 UTC Need Help??
fixu2 has asked for the wisdom of the Perl Monks concerning the following question:

How can i write a perl script that do the following: "For a given string, perform XOR by 32 bits, and take the inverse of the XOR result as a hexadecimal value"

Replies are listed 'Best First'.
Re: Perl XOR
by Happy-the-monk (Canon) on Sep 18, 2013 at 18:18 UTC

"For a given string, perform XOR by 32 bits, and take the inverse of the XOR result as a hexadecimal value"

Where have you been looking?

Cheers, Sören

Créateur des bugs mobiles - let loose once, run everywhere.
(hooked on the Perl Programming language)

Re: Perl XOR
by kennethk (Abbot) on Sep 18, 2013 at 18:18 UTC
What did you try? What worked? What didn't? This is not a code writing service, but we're happy to give pointers and help debug. See How do I post a question effectively?.

You should find some useful guidance with Bitwise Or and Exclusive Or in perlop, sprintf's %x, and hex (or possibly unpack depending on what your input string looks like).

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Perl XOR
by ikegami (Pope) on Sep 18, 2013 at 18:40 UTC

What does it mean to "XOR a string by 32 bits"? Does that mean "XOR a 4-byte string with a 32-bit value"? If so,

```unpack('N', \$s) ^ \$val   # Big-endian byte order
-or-
unpack('V', \$s) ^ \$val   # Little-endian byte order

What does it mean to "take the inverse of the XOR result as a hexadecimal value"? Hexadecimal is a string representation of a number, and strings don't have inverses.

Re: Perl XOR
by Laurent_R (Canon) on Sep 18, 2013 at 18:46 UTC

Some of the operators you may want to consider: | (bitwise or), ^ (bitwise xor), ~ (bitwise not). They should give you what you need if I understood your requirement correctly. Try perldoc op for more information on them.

Re: Perl XOR
by derby (Abbot) on Sep 19, 2013 at 01:20 UTC

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1054713]
Approved by Happy-the-monk
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2018-04-26 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (96 votes). Check out past polls.

Notices?