I am having a funny with my C++ managed code with visual 2008. I am sure there is a simple reason for this to happen but I am just missing it and getting a bit frustrated here.
The problem: I am using a XOR to evaluate a checksum of a transmitted sentence. Well, it works flawless in debugging and if I run from visual as release but when I build the installer and install the release, in the same machine, the XOR behave totally differently giving me different values. ie in debugging it gives me 44 and in installed version it gives me 79E721FC.
The code:
{
String^ OrigChecksum=mysentence->Substring(mysentence->IndexOf('*')+1);
if(mysentence->StartsWith("!"))
{
mysentence=mysentence->Substring(mysentence->IndexOf('!')+1,mysentence->IndexOf('*')-mysentence->IndexOf('!')-1);
}
else if(mysentence->StartsWith("$"))
{
mysentence=mysentence->Substring(mysentence->IndexOf('$')+1,mysentence->IndexOf('*')-mysentence->IndexOf('$')-1);
}
int checksum;
if(mysentence->Length>1)
{
for(int i=0;i<mysentence->Length;i++)
{
try
{
checksum ^= Convert::ToByte(mysentence[i]);
//MessageBox::Show(Convert::ToByte(mysentence[i]).ToString("X2"));
}
catch(...)
{
}
}
String^ strChecksum=checksum.ToString("X2");
//MessageBox::Show(OrigChecksum+","+strChecksum+","+checksum);
if(OrigChecksum==strChecksum)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
Note the MessageBox that I am using to see the values when installed as I cannot use the debugger there and when I debug in Visual studio everything is perfect... I have tried another approach with same results as follow: using a char array array^character=mysentence->ToCharArray(); and using the operator ^ instead checksum=checksum ^ int(character[num])
Did anyone have a similar experience? Please, any advice or light on this?
All the best Adam
int checksum;
is never initialised, so attempting to xor with it invokes undefined behaviour.