1
I’ve been studying C++ for a while and I want to train by rewriting functions already done by me in C# only in C++.
I wrote a simple code that computes a IV
of a Hash, very simple even, calling the same methods using the same argument in both methods the returns are different and did 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 the following:
// 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 equal, but I don’t understand why in C# the same code gives 152 and in C++ gives 50.
Somebody explain to me?
put to work in ideone: https://ideone.com/ and I would start testing by operators
+=
and*=
I don’t know how they behave in C++– Rovann Linhalis