No such thing as a small change PerlMonks

### Re^3: Hex String XOR

by tobyink (Abbot)
 on Mar 12, 2012 at 11:30 UTC ( #959110=note: print w/replies, xml ) Need Help??

in reply to Re^2: Hex String XOR

Here's a solution that works on hex strings, so allows you to work with hex numbers larger than the maximum integers Perl can handle...

```use 5.010;
use strict;
use Carp qw/croak/;
use List::Util qw/max/;

sub xor_strings
{
croak "should be passed two arguments" unless @_==2;
state \$chunk_size = 4;
state \$pattern    = sprintf '%%0%dx', \$chunk_size;

# Make strings equal length, and a multiple of \$chunk_size.
my \$length = max(map { length \$_ } @_);
\$length += \$chunk_size - (\$length % \$chunk_size);
my @strings = map { ('0'x(\$length - length \$_)) . \$_ } @_;

# Join results of each chunk
return join q{},
map {
# Parse chunk hex to an integer
my \$i = \$_;
my @nums = map { hex substr \$_, \$i*\$chunk_size, \$chunk_siz
+e } @strings;
# Xor them and convert to hex.
sprintf \$pattern, \$nums[0] ^ \$nums[1]
}
0 .. (\$length/\$chunk_size)-1;
}

say xor_strings(
'112233112233112233112233112233112233112233112233112233',
'aabbccaabbccaabbccaabbccaabbccaabbccaabbccaabbcc112233',
);

The chunk size of 4 is fairly conservative. It means that the string in processed in four-digit (i.e. 16 bit) chunks. You can probably get a minor speed up using a larger chunk size if you know that your computer will be able to handle it. The returned value will be left-padded with zeroes to be a length that is a multiple of the chunk size.

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

Replies are listed 'Best First'.
Re^4: Hex String XOR
by flexvault (Monsignor) on Mar 12, 2012 at 18:48 UTC

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

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'

Create A New User
Node Status?
node history
Node Type: note [id://959110]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2017-11-19 06:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In order to be able to say "I know Perl", you must have:

Results (278 votes). Check out past polls.

Notices?