### Re: Hex String XOR

by rovf (Priest)
 on Mar 12, 2012 at 09:25 UTC ( #959096=note: print w/replies, xml ) Need Help??

in reply to Hex String XOR

I give an input of say "aabbcc" and I want to XOR with "112233". The answer should be "bb99ff"

```my \$s='112233';
my \$t='aabbcc';
my \$x=hex(\$s)^hex(\$t);
printf('%0x', \$x);

--
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re^2: Hex String XOR
by moritz (Cardinal) on Mar 12, 2012 at 09:52 UTC

Note that this solution too is limited by the size of the biggest integer you can store in a perl scalar, which is usally 52 bit from the mantissa of a double precision floating point, or 64 bit for an integer on 64bit platforms (not sure about the sign, so could be 63 usable bits for this purpose).

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'

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

"^" converts floats to machine integers, so you're limited to 32/64, not 5253/64.

Create A New User
Node Status?
node history
Node Type: note [id://959096]
help
Chatterbox?
 [Corion]: Hmm - I would say the 5.8.5 is a broken installation / corrupt tarball download, and the 5.10.0 is really weird, and maybe a bug in that version of Perl [Corion]: I don't see how my \$result = eval q{'abc' =~ ?b?}; could create a "Modification of read-only value" error [marto]: Discipulus the issue that should be adressed is that the page needs to be updated to reflect modern perl on Windows [Discipulus]: but is really necessary to support these ancient versions? from 5.14 onward is not enough? [Discipulus]: yes marto I understood [Corion]: Discipulus: I'm slowly migrating my code to require 5.8.x ;) Most of my code works on 5.6, but Filter::signatures requires 5.10 I think [Corion]: 5.14 is also a good target, widely available and fairly stable. Also, over five years old, which doesn't push people into a needless upgrade cycle [Discipulus]: yes I saw your efforts Corion but the pain is worth? [Corion]: Discipulus: IMO it's not that much pain, but most of the systems I use have a Perl 5.14 or newer on them nowadays

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2018-06-25 08:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (126 votes). Check out past polls.

Notices?