Comparison of two hexadecimal strings in Lucee fai...
# lucee
a
Comparison of two hexadecimal strings in Lucee failed oddly, and I eventually worked out what was going wrong. What's the accepted way to compare these strings as strings, instead of "zero in scientific notation" and "zero with extra steps"?
r
compare or compareNoCase
āœ… 1
a
^^^ nailed it. @Asher Densmore-Lynn in future, pls post the actual code (or the link to the gist in trycf) instead of just a picture of it?
šŸ‘ 2
a
Sure, sorry.
Thanks!
a
It's fine in this case cos the answer was quick / easy. But if we have to mess around with yer code to try to work out what's going on, it's difficult to do with just a picture šŸ˜‰
a
Yeah, that's fair, I just figured it was going to be a quick glance and didn't know you could paste gists. Otherwise I would've sent it to Pastebin or something.
āœ… 1
e
Are you sure you are using the function correctly? This BTW is the same value in AFC 2021 Compare Description Performs a case sensitive comparison of two strings. Returns • -1, if string1 is less than string2 • 0, if string1 is equal to string2 1, if string1 is greater than string2
a
Yeah, you have to ! it to get it to work the way you want, which leaves you in the odd place of having to use a not on a function called "compare" to test for equality.
e
Um no you are trying to compare a string to a string which is NOT the same as a string to a number. The function I would argue is working correctly, https://trycf.com/gist/fbbc18de0593ddd2abdf3a8deb83fb58/lucee5?theme=monokai <-- Lucee, and AFC both do the same thing, which is the ASCII value of two strings and summed up then checked, which N1 has a higher value than N2. Your code is asking is this string to number blob is the same as this other string to number blob. Lucee, and AFC are saying, YUP String, to number blob 1 is higher than the string to number blob 2
As for comparing two or more objects without using the compare function, you can do something like: <cfset str1 = "0E5435"> <cfset str2 = "000000"> <cfif str1 EQ str2> <p>The strings are equal.</p> <cfelse> <p>The strings are not equal.</p> </cfif>
a
r
He tried that. Look at the initial screenshot. It returns true, which is incorrect.
a
Ugh, sloppy, but I rewrote it a bunch.
e
His code is stating a binary comparison, this is why I use tag, its just easier to debug https://trycf.com/gist/fba7d87812d5ff8f3639003b7b040017/lucee5?theme=monokai
šŸ¤” 1
s
While I am not sure you want to this, it does behave like I would expect, especially if you try it against Java method compareToIgnoreCase writeoutput(('0E1232'.toString() === '000000'.toString()) & "<br>");
a
Oh, neat. That's at least more idiomatic.