Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Perl XOR

by fixu2 (Initiate)
on Sep 18, 2013 at 18:04 UTC ( [id://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"

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 (Patriarch) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-19 23:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found