[SOLVED] Why would 2 strings be not equivalent if I converted them using str()?

Good evening,

I have the following code:

project.Generic.HMI.messageHandler('Generic', 'info', 'temp_uid: ' + str(temp_uid) +', uid: ' + str(uid))
    if str(temp_uid) == str(uid):
        #do something
    else:
        #do something else

My logs show the following message:

temp_uid: 0478201032325D, uid: 0478201032325D

However the code ‘something else’ is executed. Any clues as to why? The only clue(s) is the values come from 2 different database tables of what should apparently be varchar each.

Hmm, interesting. What is the actual type of the 2 different values? You can log type(temp_uid) and type(uid).

I will run the test you asked shortly. I just ran a test with the length of each variable and get 14 or 15 depending on the table the value comes from.

I executed the following query on each table:

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'myTable'
ORDER BY ORDINAL_POSITION

And received the following info:

temp_uid table (Returns length 14)
IS_NULLABLE: YES
CHARACTER_MAXIMUM_LENGTH: 24
CHARACTER_OCTET_LENGTH: 24

uid table (Returns length 15)
IS_NULLABLE: NO
CHARACTER_MAXIMUM_LENGTH: 25
CHARACTER_OCTET_LENGTH: 25

My hypothesis is the IS_NULLABLE adds a ‘hidden’ character when set to ‘NO’. Now why it doesn’t convert in Ignition correctly is a mystery to me.

Before converting to string using str() :

temp_uid: <type 'unicode'>, uid: <type 'unicode'>

After converting to string using str():

temp_uid: <type 'str'>, uid: <type 'str'>

The differences in length is suspicious.

What happens if you print the bytes:

from org.python.core.util import StringUtil
print StringUtil.toBytes(temp_uid)
print StringUtil.toBytes(uid)
project.Generic.HMI.messageHandler('Generic', 'info', 'temp_uid: ' + str(StringUtil.toBytes(temp_uid)) +', uid: ' + str(StringUtil.toBytes(uid)))

temp_uid: 
array('b', [48, 52, 55, 53, 50, 48, 49, 48, 53, 54, 51, 51, 53, 68]), 

uid: 
array('b', [48, 52, 55, 53, 50, 48, 49, 48, 53, 54, 51, 51, 53, 68, 32])

It’s a space? That gives me another clue.

Yep, don’t think there’s any mystery re: why the comparison fails.

You should probably figure out why one table has whitespace at the end but you can probably make your comparison work immediately by doing something like this:

if str(temp_uid).strip() == str(uid).strip():
    ...
2 Likes

Damn, my apologies for blaming Ignition. Thanks for the help.

I like writing log messages with quotes around things I’m not sure of - helps a lot with this class of bugs.

4 Likes