Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arithmetic operation not returning the same in VB6 and C#

I translated a VB6 module containing a couple of encryption functions into c#, I have the following aritmethic operation in both sides:

C#:

int inter, cfc;
inter = 6940;
cfc = Convert.ToInt32((((inter / 256) * 256) - (inter % 256)) / 256);
//cfc = 26

VB6:

Dim inter As long
Dim cfc As long     
inter = 6940
cfc = (((inter / 256) * 256) - (inter Mod 256)) / 256
'cfc = 27

I haven't been able to figure out the result mismatch since all operations are returning integer numbers, this is causing the encryption process to work unexpectedly.

like image 521
Lirio Lebrón Avatar asked Jan 03 '23 04:01

Lirio Lebrón


1 Answers

In C# (inter / 256) does integer division, while VB6 does not. So in one of your code samples the result of that division is being truncated to 27 before the rest of the operations, while the other uses a value of 27.109375. This is leading to the difference in your end results.

Use (inter \ 256) in VB6 if integer division is what you intend.

like image 118
Bill the Lizard Avatar answered Jan 13 '23 19:01

Bill the Lizard