2010-05-15

UTF7 and String


unit frm_Main;

interface

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

type
  TfrmMain = class(TForm)
    edtEdit1: TEdit;
    btnStrToUtf7: TButton;
    btnUtf7ToStr: TButton;
    Memo1: TMemo;
    btnClose: TButton;
    procedure btnStrToUtf7Click(Sender: TObject);
    procedure btnUtf7ToStrClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

procedure TfrmMain.btnCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmMain.btnStrToUtf7Click(Sender: TObject);
var
  vHex, vCoded: String;
  vSS: TStringStream;
  ii: Integer;
begin
  vHex := '';
  vCoded := '';
  vSS:= TStringStream.Create(edtEdit1.Text, Tencoding.UTF7);
  for ii := 0 to vSS.Size - 1 do
  begin
    vHex := vHex + Format('%.2x ', [vSS.Bytes[ii]]);
    vCoded := vCoded + Chr(vSS.Bytes[ii]);
  end;
  vSS.Free;
  Memo1.Lines.Append('Hex:' + vHex);
  Memo1.Lines.Append('Coded:' + vCoded);
  Memo1.Lines.Append('- - - - -');
end;

procedure TfrmMain.btnUtf7ToStrClick(Sender: TObject);
var
  vDecode, vHex: String;
  vSS: TStringStream;
  ii: Integer;
begin
  vHex := '';
  vDecode := '';
  vSS := TStringStream.Create('', Tencoding.UTF7);
  vSS.Size := Length(edtEdit1.Text);
  for ii := 1 to Length(edtEdit1.Text) do
  begin
    vSS.Bytes[ii-1] := Ord(edtEdit1.Text[ii]);
    vHex := vHex + Format('%.2x ', [vSS.Bytes[ii-1]]);
  end;
  vDecode := vSS.DataString;
  vSS.Free;
  Memo1.Lines.Append('Hex:' + vHex);
  Memo1.Lines.Append('Coded:' + vDecode);
  Memo1.Lines.Append('- - - - -');
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  Memo1.Lines.Clear;
end;

end.

沒有留言: