Beefy Boxes and Bandwidth Generously Provided by pair Networks kudra
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Perl XOR

by fixu2 (Initiate)
on Sep 18, 2013 at 18:04 UTC ( #1054713=perlquestion: print w/ replies, xml ) 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"

Comment on Perl XOR
Re: Perl XOR
by Happy-the-monk (Monsignor) 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 (Monsignor) 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 (Priest) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1054713]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2014-04-21 16:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (498 votes), past polls