本例效果图:
代码文件:
代码文件:
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.窗体文件:
object Form1: TForm1 Left = 0 Top = 0 Caption = 'Form1' ClientHeight = 156 ClientWidth = 353 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 13 object Memo1: TMemo Left = 0 Top = 0 Width = 145 Height = 88 Align = alLeft Lines.Strings = ( 'Memo1') ScrollBars = ssVertical TabOrder = 0 end object Memo2: TMemo Left = 157 Top = 0 Width = 196 Height = 88 Align = alRight Lines.Strings = ( 'Memo2') ScrollBars = ssVertical TabOrder = 1 end object Panel1: TPanel Left = 0 Top = 88 Width = 353 Height = 68 Align = alBottom TabOrder = 2 object Button1: TButton Left = 16 Top = 6 Width = 73 Height = 25 Caption = 'Button1' TabOrder = 0 OnClick = Button1Click end object Button2: TButton Left = 95 Top = 6 Width = 74 Height = 25 Caption = 'Button2' TabOrder = 1 OnClick = Button2Click end object Button3: TButton Left = 175 Top = 6 Width = 82 Height = 25 Caption = 'Button3' TabOrder = 2 OnClick = Button3Click end object Button4: TButton Left = 263 Top = 6 Width = 74 Height = 25 Caption = 'Button4' TabOrder = 3 OnClick = Button4Click end object Button5: TButton Left = 16 Top = 37 Width = 96 Height = 25 Caption = 'Button5' TabOrder = 4 OnClick = Button5Click end object Button6: TButton Left = 130 Top = 37 Width = 207 Height = 25 Caption = 'Button6' TabOrder = 5 OnClick = Button6Click end end end