BinHexMethods (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The purpose of this example is to illustrate the BinToHex and HexToBin methods from the Classes unit. This code will convert a binary value to its hexadecimal representation and back.

Code

procedure Tfrm1.btnToStringClick(Sender: TObject);
var
  LStr1, LStr2: WideString;

begin
  { Store the text in the memo to a String variable. }
  LStr1 := Memo2.Lines.Text;
  { Set the length of the String to hold the conversion. }
  SetLength(LStr2, Length(LStr1) div 4);
  { Call the hexadecimal to binary conversion procedure. }
  HexToBin(PWideChar(LStr1), LStr2[1], Length(LStr1) div SizeOf(Char));
  { Output the results to Memo1. }
  Memo1.Lines.Text := LStr2;
end;

procedure Tfrm1.btToHexClick(Sender: TObject);
var
  LStr1, LStr2: String;

  begin
  { Store the text in the memo to a String variable. }
  LStr1 := Memo1.Lines.Text;
  { Set the length of the String to hold the conversion. }
  SetLength(LStr2, Length(LStr1) * 4);
  { Call the binary to hexadecimal conversion procedure. }
  BinToHex(LStr1[1], PWideChar(LStr2), Length(LStr1) * SizeOf(Char));
  { Put the results in Memo2. }
  Memo2.Lines.Text := LStr2;
end;

Uses