Delphi - BinToHex and HexToBin : Reversal

18,833

Solution 1

If you want to do it yourself:

function xHexToBin(const HexStr: String): TBytes;
const HexSymbols = '0123456789ABCDEF';
var i, J: integer;
    B: Byte;
begin
  SetLength(Result, (Length(HexStr) + 1) shr 1);
  B:= 0;
  i :=  0;
  while I < Length(HexStr) do begin
    J:= 0;
    while J < Length(HexSymbols) do begin
      if HexStr[I + 1] = HexSymbols[J + 1] then Break;
      Inc(J);
    end;
    if J = Length(HexSymbols) then ; // error
    if Odd(I) then
      Result[I shr 1]:= B shl 4 + J
    else
      B:= J;
    Inc(I);
  end;
  if Odd(I) then Result[I shr 1]:= B;
end;

Solution 2

Why not just use the BinToHex and HexToBin RTL functions?

{$APPTYPE CONSOLE}

uses
  System.Classes,
  System.SysUtils;

var
  LArray : array[1..6] of Byte = (10, 11, 12, 13, 14, 15);
  LText: string;
  I : integer;
begin
  try
    SetLength(LText, Length(LArray)*2);
    BinToHex(@LArray, PChar(LText), SizeOf(LArray));
    //show the hex string
    Writeln(LText);

    //fill the array with 0 
    FillChar(LArray, SizeOf(LArray), #0);

    //get the values from the hex string
    HexToBin(PChar(LText), @LArray, Length(LArray));

    //show the array values
    for i := 1 to Length(LArray) do
      Write(LArray[i]);
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Solution 3

2 hex chars represent 1 byte

The code i wrote earlier was pseudocode I wanted you to get the ideea. But if you need some code to paste here is the actual implementation:

program project1;

uses SysUtils,classes;


type
  TByteArray = array of byte;
function StrToArray(const Hexstr: String): TByteArray ;
var
i: Integer;
begin
SetLength(Result, Length(Hexstr) div 2);
for i:=0 to (Length(Hexstr) div 2 - 1) do
        Result[i]:= StrToInt('$' + Copy(Hexstr, (i * 2) + 1, 2));
end;


var
   arr : TByteArray;
   i : Integer;
begin
  arr := StrToArray('0A0B0C0D');
  for i:=0 to High(arr) do
          WriteLn(arr[i]);
  Readln;
end.   

Btw coding is not about cuting and pasting ;)

Share:
18,833

Related videos on Youtube

Josh Line
Author by

Josh Line

Updated on September 15, 2022

Comments

  • Josh Line
    Josh Line over 1 year

    I have an array of byte that I wish to convert into a hex string, and then back into an array of byte.

    I am using the following to convert into a hex string:

    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;
    

    I am unsure how to convert it back into an array of byte properly. I am shooting for something like the following:

    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;
    

    How would I go about doing this?

    Also, I am using Delphi XE2.

    • Sertac Akyuz
      Sertac Akyuz over 11 years
      RTL has a HexToBin and BinToHex.
  • Josh Line
    Josh Line over 11 years
    I get "Incompatible types: 'Byte' and 'string'" on this Line: Result[i]:= '$' + Copy(Hexstr, (i * 2) + 1, 2);
  • opc0de
    opc0de over 11 years
    sorry don't have a compiler try now
  • Josh Line
    Josh Line over 11 years
    I cant set the function type to an array of byte...and when trying to run the function it simply crashed the application. :/
  • Josh Line
    Josh Line over 11 years
    Funny thing is, I now know. I learned something. I am still in school learning computer science. I have just begun. Simply because I did not know the answer is sad? Then every post here is "sad"? I LEARNED from the responses, and now know and understand the answer, It's sad that you have to be rude for no reason. I did try, thanks.