我将一个库从C++移植到C#,但遇到了一个我不确定如何解决的情况,涉及将unsigned char *
转换为unsigned int *
。
C++
unsigned int c4;
unsigned int c2;
unsigned int h4;
int pos(unsigned char *p)
{
c4 = *(reinterpret_cast<unsigned int *>(p - 4));
c2 = *(reinterpret_cast<unsigned short *>(p - 2));
h4 = ((c4 >> 11) ^ c4) & (N4 - 1);
if ((tab4[h4][0] != 0) && (tab4[h4][1] == c4))
{
c = 256;
return (tab4[h4][0]);
}
c = 257;
return (tab2[c2]);
}
C#(这是错误的):
public uint pos(byte p)
{
c4 = (uint)(p - 4);
c2 = (ushort)(p - 2);
h4 = ((c4 >> 11) ^ c4) & (1 << 20 - 1);
if ((tab4[h4, 0] != 0) && (tab4[h4, 1] == c4)) {
c = 256;
return (tab4[h4, 0]);
}
c = 257;
return (tab2[c2]);
}
我认为在C#示例中,您可以将byte p
更改为byte[]
,但是当涉及到将byte[]
转换为单个uint值时,我感到困惑。
此外,有人能解释一下,为什么要将unsigned char *
强制转换为unsigned int *
吗?它有什么用途?
任何帮助/指引都将非常有用。
i
跳过了前四个字节,并以 6 递增?能展示一部分吗?我建议重新编写文件解析器,使用反映Int32
后跟ushort
布局的结构体,或者可能只需在pos()
中有两个参数。我对重复使用reinterpret_cast
的争议并不是我们在这里的重点。 - Cee McSharpface