gpt4 book ai didi

delphi - delphi 如何将大端数字转换为 native 数字

转载 作者:行者123 更新时间:2023-12-03 14:55:16 27 4
gpt4 key购买 nike

我想知道如何在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com