频道分类

Delphi 查看字符串在不同编码(ASCII、Unicode、UTF7、UTF8、Default、BigEndianUnicode)下的 Hex

作者:admin 来源: 日期:2020/10/15 19:24:00 人气: 标签:

 
代码文件:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Memo2: TMemo;
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{从字符串到十六进制的函数}
function StrToHex(str: string; AEncoding: TEncoding): string;
var
  ss: TStringStream;
  i: Integer;
begin
  Result := '';
  ss := TStringStream.Create(str, AEncoding);
  for i := 0 to ss.Size - 1 do
    Result := Result + Format('%.2x ', [ss.Bytes[i]]);
  ss.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo2.Text := StrToHex(Memo1.Text, TEncoding.ASCII);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Memo2.Text := StrToHex(Memo1.Text, TEncoding.Unicode);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  Memo2.Text := StrToHex(Memo1.Text, TEncoding.UTF7);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  Memo2.Text := StrToHex(Memo1.Text, TEncoding.UTF8);
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  Memo2.Text := StrToHex(Memo1.Text, TEncoding.Default);
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  Memo2.Text := StrToHex(Memo1.Text, TEncoding.BigEndianUnicode);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Button1.Caption := 'To ASCII';
  Button2.Caption := 'To Unicode';
  Button3.Caption := 'To UTF7';
  Button4.Caption := 'To UTF8';
  Button5.Caption := 'To Default';
  Button6.Caption := 'To BigEndianUnicode';
end;

end.