Pav*_*van 1 delphi delphi-2009
我有TBytes变量值[0,0,15,15].如何将其转换为"00FF"?
我不想使用循环,bcoz这个逻辑用于时间密集的功能.
(我尝试使用BinToHex,但我无法使用字符串变量.)
感谢和问候,
帕文.
// Swapping is necessary because x86 is little-endian.
function Swap32(value: Integer): Integer;
asm
bswap eax
end;
function FourBytesToHex(const bytes: TBytes): string;
var
IntBytes: PInteger;
FullResult: string;
begin
Assert(Length(bytes) = SizeOf(IntBytes^));
IntBytes := PInteger(bytes);
FullResult := IntToHex(Swap32(IntBytes^), 8);
Result := FullResult[2] + FullResult[4] + FullResult[6] + FullResult[8];
end;
Run Code Online (Sandbox Code Playgroud)
如果最后一行看起来有点奇怪,那是因为您要求将四字节数组转换为四字符字符串,而在一般情况下,需要八个十六进制数字来表示四字节值.我只是假设你的字节值都低于16,所以只需要一个十六进制数字.如果您的示例是拼写错误,那么只需用这一行替换最后两行:
Result := IntToHex(Swap32(IntBytes^), 8);
Run Code Online (Sandbox Code Playgroud)
顺便说一句,你的禁止循环的要求将无法满足.IntToHex
在内部使用循环.
归档时间: |
|
查看次数: |
17880 次 |
最近记录: |