Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^4: Hex String XOR

by flexvault (Monsignor)
on Mar 12, 2012 at 18:48 UTC ( [id://959158]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Hex String XOR
in thread Hex String XOR

tobyink,

Reading the Camel book (3rd and 4th editions), "...if both operands are strings...the operators do bitwise operations between corresponding bits from the two strings. In this case there's no arbitrary limit..."

So the following code should work (untested) and be faster than calling a subroutine and looping:

my $result = '112233112233112233112233112233112233112233112233112233' +^ 'aabbccaabbccaabbccaabbccaabbccaabbccaabbccaabbcc112233';

I used this technique to generate a 8-byte CRC for arbitrary text strings by using 'substr' to take 8-byte substrings. I needed the looping for that.

Regards...Ed

"Well done is better than well said." - Benjamin Franklin

Replies are listed 'Best First'.
Re^5: Hex String XOR
by tobyink (Canon) on Mar 13, 2012 at 01:19 UTC

    Yes, ^ does work on strings, but as the OP mentioned, it works on the string's bytes, not the hexadecimal numbers encoded in the string.

    $ perl -E"say '112233'^'aabbcc'" PPPPPP $ perl -E"say sprintf '%x', 0x112233 ^ 0xaabbcc" bb99ff

    My code gives the latter behaviour (which the OP wants) on hexadecimal strings of arbitrary length.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-03-19 05:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found