# XOR for String or pack/unpack for Bignum

**URL:** <https://rubytalk.org/t/xor-for-string-or-pack-unpack-for-bignum/4878>\
**Category:** ruby-talk\
**Created:** [24 February 2003 19:10 UTC](https://rubytalk.org/t/xor-for-string-or-pack-unpack-for-bignum/4878 "2003-02-24T19:10:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Bruschkewitz](https://avatars.discourse-cdn.com/v4/letter/m/b4bc9f/32.png) [@Michael\_Bruschkewitz](https://rubytalk.org/u/Michael_Bruschkewitz)\
**Post date:** [24 February 2003 19:10 UTC](https://rubytalk.org/t/xor-for-string-or-pack-unpack-for-bignum/4878/1 "2003-02-24T19:10:09Z")

</div>

Hello, I’m doing some en-/de-cryptions based on DES on String data.  
For this, I everytime need 64-Bit blocks XOR’ed together.  
But there is no XOR-operation on String data.  
Before I start building a C-Extension for String here, I just want to  
ask if there is any slightly effective possibility to do this better  
than:  
0.upto(7){|i| a[i] = (a[i].to\_i ^ s[i+ofs].to\_i).chr}

I’ve already considered converting int array of longs or into bignum,  
but I’m looking for a more elegant solution.

Btw. Bignum is not to be seen in the list of directives for pack/unpack,  
wouldn’t it be nice to have such an conversion?

Michael B.

---

<div class="post-metadata">

**Author:** ![Warren\_Brown3](https://avatars.discourse-cdn.com/v4/letter/w/9d8465/32.png) [@Warren\_Brown3](https://rubytalk.org/u/Warren_Brown3)\
**Post date:** [24 February 2003 20:02 UTC](https://rubytalk.org/t/xor-for-string-or-pack-unpack-for-bignum/4878/2 "2003-02-24T20:02:29Z")

</div>

Michael,

> I just want to ask if there is any slightly effective  
> possibility to do this better than:  
> 0.upto(7){|i| a[i] = (a[i].to\_i ^ s[i+ofs].to\_i).chr}

```
One trivial improvement: String#[](aFixnum) returns a Fixnum, not a

```

one-character String (String#= behaves similarly). So:

0.upto(7) {|i| a[i] ^= s[i+ofs] }

```
I realize this isn't what you were looking for, but every little bit

```

helps.

```
- Warren Brown

```
