olmanRvr

stringcompare getting seemingly incorrect result

8 posts in this topic

(code given below) I m trying to compare the physical hard drive number returned by a autoit function to the same number hard coded in to the script by using stringcompare and also “==” operator.I expected the result of the comparison would produce a “match” but stringcompares returns non-zero(not equal).However consolewrite outputs the same values.Cant figure out what the mistake is.

Will greatly appreciate any help

Thanks

olmar

#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <Date.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

chkCond()

Func chkCond()
    Local $constSr="2020202020202020202059205337544c41335452"
    Local $getSr=String(gtSrNo())
    Local $ans
          $ans= StringCompare($constSr, $getSr)
          MsgBox(0,0,"const str is:"&$constSr&@CRLF&"machi str is:"&$getSr)
          MsgBox(0,0,"Result is: "&$ans&@CRLF)

    ConsoleWrite("The machine serial is:--------- "&$getSr&@CRLF)
    ConsoleWrite("The CONST serial is:--------- "&$constSr&@CRLF)

If  $constSr==$getSr Then
    MsgBox(0,0,"Same")
Else
    MsgBox(0,0,"Not Same")
EndIf
EndFunc

Func gtSrNo();returns physical hard disc serial num
$wbemFlagReturnImmediately = 0x10
 $wbemFlagForwardOnly = 0x20
 $colItems = ""
 $strComputer = "localhost"
  $Output=""
 $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
 $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
 If IsObj($colItems) then
    For $oItm In $colItems
       $strPowerManagementCapabilities = $oItm.PowerManagementCapabilities(0)
       $Output = $Output & $oItm.SerialNumber & @CRLF
    Next
Else
    Msgbox(0,"WMI Output","Pl No WMI Objects Found for class: " & "Win32_DiskDrive" )
Endif
 Return $Output
 EndFunc

 

Share this post


Link to post
Share on other sites



olmanRvr,

Try comparing the 2 strings converted to binary with StringToBinary and see if they match - there are often non-printing characters which mess up normal string comparisons.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

It could be due to a mismatch in case sense. The two comparisons are different because the comparison operator "==" performs a case sensitive comparison in AutoIt. Try a single equals sign and see if that produces the result you expect. Or set StringCompare() to perform a case sensitive comparison.

Edited by czardas

Share this post


Link to post
Share on other sites
13 hours ago, Melba23 said:

olmanRvr,

Try comparing the 2 strings converted to binary with StringToBinary and see if they match - there are often non-printing characters which mess up normal string comparisons.

M23

Hi,

As per your suggestion changed the values to binaries:-

$constSr=StringToBinary($constSr)
$getSr=StringToBinary($getSr)

But again the strings don't match.Also tried czardas's suggestion for using "=" instead of "==" to compare the strings.No success.I noticed if the  constant  $constSr is assigned the value from a function call it works(code below).But for my purpose I need to hard code it.Any suggestion please.

thanks

chkCond()

Func chkCond()
    Local Const $constSr=gtSrNo2();assigns value instead of hard coding

    Local $getSr=gtSrNo()

    Local $ans
          $ans= StringCompare($constSr, $getSr)
          If $ans==0 Then
                MsgBox(0,0,"same")
          Else
                MsgBox(0,0,"different")
          EndIf
    ConsoleWrite("The machine serial is:--------- "&$getSr&@CRLF)
    ConsoleWrite("The CONST serial is:--------- "&$constSr&@CRLF)

If  $constSr==$getSr Then
    MsgBox(0,0,"Same")
Else
    MsgBox(0,0,"Not Same")
EndIf
EndFunc


Func gtSrNo();returns physical hard disc serial num
$wbemFlagReturnImmediately = 0x10
 $wbemFlagForwardOnly = 0x20
 $colItems = ""
 $strComputer = "localhost"
  $Output=""
 $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
 $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive", "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
 If IsObj($colItems) then
    For $oItm In $colItems
       $strPowerManagementCapabilities = $oItm.PowerManagementCapabilities(0)
       $Output = $Output & $oItm.SerialNumber & @CRLF
    Next
Else
    Msgbox(0,"WMI Output","Pl No WMI Objects Found for class: " & "Win32_DiskDrive" )
Endif



 Return $Output
EndFunc

;Func gtSrNo2() is exact copy of "Func gtSrNo()" .Copy paste Func gtSrNo() below

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Try removing the longer string from the shorter string and see if you are left with anything: perhaps CRLF. If the strings are the same length, then try the comparing the string one character at a time until you hit a mismatch. Examine the binary of each string. Post a working reproducer - i.e. all the code.

Edited by czardas

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Change this line and it works when compared to the (non-binary) hard coded string value:

Local $getSr= StringStripWS(gtSrNo(), 8)

Match

The machine serial is:--------- WD-WCC3F4TKK622
The CONST serial is:--------- WD-WCC3F4TKK622

 

Edited by czardas

Share this post


Link to post
Share on other sites
17 minutes ago, czardas said:

Change this line and it works when compared to the hard coded version (non-binary):

Local $getSr= StringStripWS(gtSrNo(), 8)

 

Hi czardas,

Absolutely great!!! It is working!! This is what i wanted.Issue SOLVED

Changed assignment of $getStr FROM  $getStr=getStr() TO $getSr= StringStripWS(gtSrNo(), 8)

Thanks a whole lot

olmar

1 person likes this

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

You are welcome. I guess it's fine as it is, although might be better to only strip leading and trailing white spaces (1+2). I just put the value 8 to strip all spaces as a quick test. Take a look at StringStripWS() parameter options in the help file. It's good to know for the future anyway! :)

... and please mark the topic solved.

Edited by czardas

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now