The long
type should not be printed with %d
, this is explicitly undefined in the specification. For example,
Each conversion specification is introduced by the '%' character ...
...
If a conversion specification does not match one of the above forms, the behavior is undefined. If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined. http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html
(the documentation applies to printf
as well as fprintf
)
Thus you cannot rely on the output of,
printf("id = %d remotestep = %d \n", GetLong(Packet->Message, 2), RemoteStep);
to determine that
printf("equals = %d \n", GetLong(Packet->Message, 2) == RemoteStep);
//output: equals = 0
is in fact incorrect, and you will need to fix your debug statements first:
printf("id = %ld remotestep = %ld \n", GetLong(Packet->Message, 2), RemoteStep);
In your case it is also preferable to use memcpy
instead of the pointer cast,
long GetLong(BYTE * Message, const SHORT Offset)
{
long result;
std::memcpy(&result, &(Message[Offset]), sizeof(long));
return result;
}
since long*
can have a stricter alignment requirement on your platform than a char*
.