CFLib.org – Common Function Library Project

strXOR(str1, str2)

Last updated December 01, 2006

author

Peter Day

Version: 1 | Requires: CF6 | Library: StrLib

Description:
Returns as a binary object the character by character exclusive OR of two given strings for a length of the shortest of the two. The result is the same as php: $str1 ^ $str2

Return Values:
Returns a binary string.

Example:

<cfset str1="abc">
<cfset str2="xyzdef">
<cfset x=BinaryEncode(strXOR(str1,str2),"hex")>
<cfoutput>
#x#
</cfoutput>

Parameters:

Name Description Required
str1 First string. Yes
str2 Second string. Yes

Full UDF Source:

/**
 * Returns as a binary object the chr by chr XOR of two given strings for length of shortest of the two.
 * 
 * @param str1      First string. (Required)
 * @param str2      Second string. (Required)
 * @return Returns a binary string. 
 * @author Peter Day (JWVPICBHZCOX@spammotel.com) 
 * @version 1, December 1, 2006 
 */
function strXOR(str1,str2) {
    var theXOR="";
    var minLen=min(len(str1),len(str2)); 
    var i=1;
    
    for (; i le minLen; i=i+1 ) {
        theXOR = theXOR & rJustify(formatbaseN(bitXor(asc(mid(str1,i,1)),asc(mid(str2,i,1))),"16"),2);
    }
    
    theXOR=binaryDecode(replaceNoCase(theXOR," ","0","all"),"hex");
    return theXOR;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson