Delphi - BinToHex et HexToBin : Inversion

J'ai un tableau de byte que je souhaite convertir en une chaîne hexadécimale, et ensuite de retour dans un tableau de byte.

Je suis l'aide de le convertir en une chaîne hexadécimale:

function bintoHex(const bin: array of byte): String;
const HexSymbols = '0123456789ABCDEF';
var i: integer;
begin
  SetLength(Result, 2*Length(bin));
  for i :=  0 to Length(bin)-1 do begin
    Result[1 + 2*i + 0] := HexSymbols[1 + bin[i] shr 4];
    Result[1 + 2*i + 1] := HexSymbols[1 + bin[i] and $0F];
  end;
end;

Je ne sais pas comment faire pour le convertir en arrière dans un tableau de byte correctement. Je suis en tournage pour quelque chose comme ce qui suit:

Type TDCArray = Array of Byte;

function xHexToBin(const HexStr: String): TDCArray;
const HexSymbols = '0123456789ABCDEF';
var i: integer;
begin
  SetLength(Result, ???); //Need to now get half of the length, unsure how to go about that
  for i :=  0 to Length(HexStr)-1 do begin
    //Here convert back into an array somehow...
    //Result[i] := ???
  end;
end;

Comment pourrais-je aller sur le faire?

Aussi, je suis en utilisant Delphi XE2.

  • RTL a une HexToBin et BinToHex.
InformationsquelleAutor Josh Line | 2012-10-19