熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Delphi編程 >> 正文

Delphi把流中的字符串轉換為UTF格式

2022-06-13   來源: Delphi編程 

  本例效果圖

  

  代碼文件

  unit Unit;

  interface

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

  type
  TForm = class(TForm)
    Button: TButton;
    Memo: TMemo;
    procedure ButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  end;

  var
  Form: TForm;

  implementation

  {$R *dfm}

  procedure TFormButtonClick(Sender: TObject);
var
  streamstream: TStringStream;
  b: Byte;
  bs: string;
begin
  {建立第一個流 使用默認的雙字節編碼; 流中的數據是 Memo 中的字符串}
  stream := TStringStreamCreate(MemoText );

  {把第一個流的十六進制編碼顯示在 Memo 中}
  bs := ;
  for b in streamBytes do bs := Format(bs + %x [b]);
  MemoLinesAdd(bs);

  {建立第二個流 用 UTF 編碼; 還是基於第一個流中的字符串}
  stream := TStringStreamCreate(streamDataString TEncodingUTF);

  {把第二個流的十六進制編碼顯示在 Memo 中}
  bs := ;
  for b in streamBytes do bs := Format(bs + %x [b]);
  MemoLinesAdd(bs);

  streamFree;
  streamFree;
end;

  procedure TFormFormCreate(Sender: TObject);
begin
  MemoAlign := alTop;
  MemoScrollBars := ssBoth;
  MemoText := 萬一的 Delphi 博客;
end;

  end

  窗體文件:

  object Form: TForm
  Left =
  Top =
  Caption = Form
  ClientHeight =
  ClientWidth =
  Color = clBtnFace
  FontCharset = DEFAULT_CHARSET
  FontColor = clWindowText
  FontHeight =
  FontName = Tahoma
  FontStyle = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch =
  TextHeight =
  object Button: TButton
    Left =
    Top =
    Width =
    Height =
    Caption = Button
    TabOrder =
    OnClick = ButtonClick
  end
  object Memo: TMemo
    Left =
    Top =
    Width =
    Height =
    LinesStrings = (
      Memo)
    TabOrder =
  end
end


From:http://tw.wingwit.com/Article/program/Delphi/201311/24665.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.