Archive: Convert string to integer?


Convert string to integer?
I am trying to read a text file to get a version number from it. I then want to compare that to the current version and abort if a newer version is already installed. This is what I have so far:

; check existing version
Push 1
Push "$INSTDIR\config\version.txt"
Call ReadFileLine
Pop $0

${If} $0 > 730
MessageBox MB_OK "A newer version of blah is already installed on this machine. Please uninstall it first."
Abort
${EndIf}

The problem is that $0 is a string and I can't find anywhere in the documentation how to convert it to an integer. I don't want to compare strings because 730 would be bigger than 1000.

How do I convert $0 to an integer?


out of curiosity - have you tried your code with $0 being a value of 1000 ('as a string')?

It should get cast to an integer automagically.


!include "LogicLib.nsh" ; Logical Library
outFile "installer.exe"
section
${If} "1000" > 730
messagebox MB_OK "A"
${Else}
messagebox MB_OK "B"
${EndIf}
sectionEnd


Pops up "A" for me.

But if in doubt, you could try...
IntOp $0 $0 + 0

I think in NSIS all variables are strings. And if you run an integer operation, the string will be converted to an integer implicitly. If you create a custom Plugin for example, you have to pop all arguments from the stack as strings. So if you need integers as input, you will have to convert the strings to integers manually. Also return values are pushed on the stack as strings, even if your output is an integer...


Thanks!


See also VersionConvert and VersionCompare headers in the NSIS help (Appendix E).

VersionConvert will convert a string into a version number for those time when a version number contains a letter (ie "1.1a")

VersionCompare compares the version numbers.