Archive: Having an unsigned int


Having an unsigned int
I'm trying to translate a C++ function to NSIS, and I've had some help with that earlier on the forums.

Here's the C++ method:

int calculateId(LPTSTR email)
{
unsigned int x = 0;

for (int i = 0; i < strlen(email); i++) {
x = x * 101;
x = x + towlower(email[i]);
}

return x;
}


and here's the core of the NSIS function:
StrCpy $3 0   
System::Call "*(&w1024 'email@someplace.com')i.r0"
StrCpy $1 $0
System::Call "*$0(&i2.r2)"
${While} $2 != 0
IntOp $3 $3 * 101
IntOp $3 $3 + $2
IntOp $0 $0 + 2
System::Call "*$0(&i2.r2)"
${EndWhile}
System::Free $1


These two methods seem to be giving two different results. Calling calculateId(email@somplace.com) gives "2849034273" where as in NSIS it is giving "-1319540362". However, I've tried debugging and it seems that when you call the two functions with the first few characters, they give the same result. Once the string becomes longer, NSIS starts giving negative values. Now I'm no NSIS expert, so I was wondering if it had to do with the nature of variables in NSIS or something else. Thank you.

You can use IntFmt (IntFmt $0 %u $0) to get an unsigned number representation. If you want to compare unsigned numbers, use IntCmpU.