CFLib.org – Common Function Library Project

strXOR(str1, str2)

Last updated December 1, 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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson