Разворот байт в HEX

Подскажите, пожалуйста, как в Delphi развернуть HEX число

Имеем число в Hex например: 2300FFC0

Нужно его развернуть: C0FF0023


Ответы (2 шт):

Автор решения: MBo

Несколько из возможных способов:

procedure TForm2.Button20Click(Sender: TObject);
var
  a, b, c, d, e: DWord;

function ReverseD0(i: DWord): DWord;  //32 и 64 bit
asm
   {$IFDEF CPUX64}
   mov eax, i
   {$ENDIF}
   bswap eax
end;

function ReverseD1(i: DWord): DWord;
begin
  Result := ((i and $FF) shl 24) or
            (((i shr 8) and $FF) shl 16) or
            (((i shr 16) and $FF) shl 8) or
            (((i shr 24) and $FF));
end;

function ReverseD2(i: DWord): DWord;
begin
  Longrec(Result).Bytes[0] := Longrec(i).Bytes[3];
  Longrec(Result).Bytes[1] := Longrec(i).Bytes[2];
  Longrec(Result).Bytes[2] := Longrec(i).Bytes[1];
  Longrec(Result).Bytes[3] := Longrec(i).Bytes[0];
end;

function ReverseD3(i: DWord): DWord;
var
  c: Integer;
begin
  for c := 0 to 3 do
     PByteArray(@Result)[c] := PByteArray(@i)[3-c];
end;

begin
  a := $AABBCCDD;
  b := ReverseD0(a);
  c := ReverseD1(a);
  d := ReverseD2(a);
  e := ReverseD3(a);
  Memo1.Lines.Add(Format('%x %x %x %x %x', [a, b, c, d, e]));
end;

>>>  AABBCCDD DDCCBBAA DDCCBBAA DDCCBBAA DDCCBBAA

"hex" - это способ представления чисел для человека, и я полагаю, что у вас просто число, а не строка.

→ Ссылка
Автор решения: Oopss
program Reverse;

var
  hexValue, x, r: Int64;
//***********************************************************************  
 //Не знаю как с этим компилятором вывести hex 
 //для вывода эта функция
function IntToHex(Value: Int64): string;
var pos:integer;
    HexChars:string;
    Result:string;
begin 
Result:='';
while Value>0 do
  begin
   HexChars := '0123456789ABCDEF';
   pos:=Value mod $10;
   Result := HexChars[pos + 1] + Result;
   Value:=Value div $10;
  end;
  IntToHex:= Result;
end;
//************************************************************************
// программа  реверс
begin
  hexValue := $2300FFC0;// Оставим исходное число для вывода, по желанию
  x:= hexValue;
  r := 0;
  while x > 0 do 
  begin
    r := r * $100 + (x mod $100); // остаток от деления прибавляем к перевертышу 
    x := x div $100; 
  end;
  writeln('hexValue  ',IntToHex(hexValue));
  writeln('Reverse   ',IntToHex(r));


end.

Output:

hexValue  2300FFC0
Reverse   C0FF0023

https://onecompiler.com/pascal/42xjbtcsm

→ Ссылка