频道分类

Delphi 二进制值转换十六进制 BinToHex和HexToBin

作者:admin 来源: 日期:2020/1/19 1:01:15 人气: 标签:

 

//本示例的目的是说明Classes单元中的BinToHex和HexToBin方法。此代码会将二进制值转换为其十六进制表示形式并返回。

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;