I'm studying C ++ a short time and I want to train rewriting functions already done by me in C # only in C ++.
I wrote a simple code that calculates a IV
of a hash, very simple even, calling the same methods using the same argument in both methods the returns are different and I do not understand why.
CalcIV(byte[] data)
in C # :
public static byte CalcIV(byte[] pass) {
int iv = 0;
foreach(byte k in pass) {
iv += k;
iv *= k + 1;
}
return (byte)(iv / pass.Length);
}
calc_iv(byte buffer[])
in C ++ :
#include <iostream>
typedef unsigned char byte;
byte calc_iv(byte buffer[])
{
int iv = 0;
size_t len = sizeof(buffer); // get buffer size
for(int i = 0; i < len; i++) {
byte I = buffer[i];
iv += I;
iv *= I + 1;
}
return iv / (len);
}
And what is returned is as follows:
// Em C#
byte[] data = new byte[] {41, 32, 16};
return CalcIV(data);
// Resultado é 152
// Em C++
byte buf[] = {41, 32, 16};
byte iv = calc_iv(buf);
return iv;
// Resultado é 50
Both results should be the same, but I do not understand why in C # the same code gives 152 and C + + gives 50 .
Does anyone explain me?