我想知道如何在 Delphi 中将大端数字转换为 native 数字。我正在移植一些我遇到的 C++ 代码:
unsigned long blockLength = *blockLengthPtr++ << 24;
blockLength |= *blockLengthPtr++ << 16;
blockLength |= *blockLengthPtr++ << 8;
blockLength |= *blockLengthPtr;
unsigned long dataLength = *dataLengthPtr++ << 24;
dataLength |= *dataLengthPtr++ << 16;
dataLength |= *dataLengthPtr++ << 8;
dataLength |= *dataLengthPtr;
我不熟悉C++,所以我不明白那些运算符的作用。
最佳答案
Andreas 的答案是一个很好的例子,说明了如何用纯 pascal 实现这一点,但它看起来仍然有点尴尬,就像 C++ 代码一样。这实际上可以在一条汇编指令中完成,但具体取决于您使用的是 32 位还是 16 位整数:
function SwapEndian32(Value: integer): integer; register;
asm
bswap eax
end;
function SwapEndian16(Value: smallint): smallint; register;
asm
rol ax, 8
end;
关于delphi - delphi 如何将大端数字转换为 native 数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3065335/