C# How to represent (unsigned long)(unsigned int) (ulong)(uint) with XOR (^)
Question
I'm converting C++ code to C#
Say I got this in C++
int v5;
v5 = (*(_DWORD *)(v4 + 68) ^ (unsigned __int64)(unsigned int)(*(_DWORD *)(v4 + 56) ^ *(_DWORD *)(v4 + 20))) % 9;
In C# it would be like..
int v5;
v5 = (int)((BitConverter.ToInt32(v4, 68) ^ (ulong)(uint)(BitConverter.ToInt32(v4, 56) ^ BitConverter.ToInt32(v4, 20))) % 9);
But I get errors.. with the (ulong)
, (uint)
Operator '^' cannot be applied to operands of type 'int' and 'ulong'
Should I do
(int)(ulong)(uint)(...)
or what?
Solution
Use BitConvert.ToUInt32
(corresponds to the DWORD
type from Win32 C++ programming) instead of ToInt32
(normally an int
), that should help you lose some of the casts and fix some of the type problems.
A faithful conversion should be something like the following, though I don't think it's necessary to have the intermediate cast to ulong
(Since the 32 most significant bits are ignored when we do the mod 9 anyway and I think they'll always end up as 0):
(int)((BitConvert.ToUInt32(v4, 68) ^ (ulong)BitConvert.ToUInt32(v4, 56) ^ BitConvert.ToUInt32(v4, 20)) % 9);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow