Evaluating Binary Values In ColdFusion

Evaluating Binary Values In ColdFusion

[ad_1]

I’ve a MySQL database desk by which I am storing a binary token (ie, a byte array). This binary token is definitely handed from ColdFusion to the database. And, the database will thankfully evaluate binary values inside of an finished SQL remark. However, when that binary token is in a ColdFusion context, there is not any local technique to evaluate that binary token to every other binary worth. As such, I sought after to take a look at two imaginable tactics to match binary values in ColdFusion.

The normal approach by which I evaluate binary values in ColdFusion is to first encode them as a string; and, then, evaluate the string values. When encoding binary values as a string, the binaryEncode() serve as helps hex and base64. It’s a must to take into account that the equality operator (==) in ColdFusion is case-insensitive. As such, it is best possible to make a choice hex as the objective encoding since hex may be case-insensitive:

<cfscript>

	// Generate binary values / byte arrays.
	a = charsetDecode( "hi international", "utf-8" );
	b = charsetDecode( "hi international", "utf-8" );
	// C has other personality casing.
	c = charsetDecode( "HELLO WORLD", "utf-8" );

	// ------------------------------------------------------------------------------- //
	// ------------------------------------------------------------------------------- //

	echo( "<h2> Convert to Strings </h2>" );
	// CAUTION: In ColdFusion, the equality operator is case-INSENSITIVE. As such, I am
	// the usage of HEX as a way to take away personality casing as a imaginable factor. I can have
	// additionally transformed to base64 after which used the evaluate() serve as.
	sell off( binaryEncode( a, "hex" ) == binaryEncode( b, "hex" ) );
	sell off( binaryEncode( a, "hex" ) == binaryEncode( c, "hex" ) );

</cfscript>

On this ColdFusion code, a and b grasp the similar binary worth since they are each constructed from the similar string. c is other. And, once we run this ColdFusion code, and convert every binary worth to a hex string, we get the next output:

Evaluating Binary Values In ColdFusion

As you’ll be able to see, a and b had been discovered to be equivalent whilst a and c had been discovered to be other.

Apart: As an experiment, I sought after to take a look at passing the binary values to the evaluate() serve as. The evaluate() serve as works on string inputs; however, it would possibly not “ruin” whilst you go it a binary worth. It is because Lucee CFML is casting the binary worth to a string at the back of the scenes. As such, it is going to evaluate two binary values. However, I am not positive if this may increasingly all the time get you the required end result—I do not need the important grab on character-encoding to understand what caveats and edge-cases to seem out for.

Whilst ColdFusion does not have a local technique to evaluate binary values, ColdFusion is constructed on most sensible of Java. As such, it is imaginable that Java has a local approach to do that. And, after some Googling, I came upon that it does. Within the java.util.Arrays elegance, there are static strategies for evaluating all varieties of array sorts; together with, byte[], which is what a binary worth in ColdFusion is.

This is the similar comparability from above, however the usage of Arrays.equals() to match the 2 binary values:

<cfscript>

	// Generate binary values / byte arrays.
	a = charsetDecode( "hi international", "utf-8" );
	b = charsetDecode( "hi international", "utf-8" );
	// C has other personality casing.
	c = charsetDecode( "HELLO WORLD", "utf-8" );

	// ------------------------------------------------------------------------------- //
	// ------------------------------------------------------------------------------- //

	// In Java, the Arrays application elegance accommodates many application strategies for evaluating
	// several types of arrays, together with byte-arrays (ie, binary values).
	Arrays = createObject( "java", "java.util.Arrays" );

	echo( "<h2> Use Array Mirrored image </h2>" );
	sell off( Arrays.equals( a, b ) );
	sell off( Arrays.equals( a, c ) );

</cfscript>

And, once we run this ColdFusion code and dip down into the Java layer, we get the next output:

Results of binary comparison by way of java.util.Arrays in ColdFusion; shows True, then False.

As you’ll be able to see, a and b had been discovered to be equivalent whilst a and c had been discovered to be other.

Below the hood, the Array.equals() manner is solely evaluating the lengths of the 2 byte arrays. And, if they are the similar, it then loops over the arrays and compares every corresponding byte component. Understanding this, it would be amusing to take a look at and write a binaryCompare() serve as in ColdFusion. However, I’m going to save that for every other day.

Wish to use code from this submit?
Take a look at the license.



[ad_2]

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back To Top
0
Would love your thoughts, please comment.x
()
x