C# 如何用 XOR (^) 表示 (unsigned long)(unsigned int) (ulong)(uint)
我正在将 C++ 代码转换为 C#
假设我在 C++ 中得到了这个,
int v5;
v5 = (*(_DWORD *)(v4 + 68) ^ (unsigned __int64)(unsigned int)(*(_DWORD *)(v4 + 56) ^ *(_DWORD *)(v4 + 20))) % 9;
在 C# 中它会像..
int v5;
v5 = (int)((BitConverter.ToInt32(v4, 68) ^ (ulong)(uint)(BitConverter.ToInt32(v4, 56) ^ BitConverter.ToInt32(v4, 20))) % 9);
但我收到错误.. 使用 (ulong)
、 (uint)
运算符“^”不能应用于“int”和“ulong”类型的操作数
我应该做什么
(int)(ulong)(uint)(...)
?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
BitConvert.ToUInt32
(对应于 Win32 C++ 编程中的DWORD
类型)而不是ToInt32
(通常是int
),这应该可以帮助您丢失一些强制转换并修复一些类型问题。忠实的转换应该类似于以下内容,尽管我认为没有必要将中间转换为
ulong
(因为无论如何,当我们执行 mod 9 时,32 个最高有效位都会被忽略,并且我认为它们最终总是 0):Use
BitConvert.ToUInt32
(corresponds to theDWORD
type from Win32 C++ programming) instead ofToInt32
(normally anint
), 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):