Delphi操作Unicode字元

來源:互聯網
上載者:User

===================================Delphi:===================================================
目前的版本(2007)中的預設狀態下, String 就是 AnsiString
在 Delphi 2009 中:
string = UnicodeString; (同樣: PString = PUnicodeString;)
Char = WideChar; (同樣: PChar = PWideChar;)

procedure
var
List: TStrings;
begin
List := TStringList.Create;
List.Text := str;
List.SaveToFile(FilePath, TEncoding.ASCII);
List.LoadFromFile(FilePath, TEncoding.ASCII); // TEncoding.UTF8;
Memo2.Lines := List;
List.Free;
end;

請教下萬老師:我發現delphi2009裡用idhttp來擷取網頁,如果網頁源碼是utf8的話可以直接用htmsrc:=idhttp.get(url),而如果是gb碼的話只能用stream方式。但是用stream取得的gb編碼網頁怎樣把他轉成utf呢。如果不轉的話在非中文作業系統下會亂碼。
procedure TForm1.Button1Click(Sender: TObject);
var
stream1,stream2: TStringStream;
b: Byte;
bs: string;
begin
{建立第一個流, 使用預設的雙位元組編碼; 流中的資料是 Memo 中的字串}
stream1 := TStringStream.Create(Memo1.Text, 54936);
{把第一個流的十六進位編碼顯示在 Memo 中}
bs := '';
for b in stream1.Bytes do bs := Format(bs + '%2x ', [b]);
Memo1.Lines.Add(bs);
stream2 := TStringStream.Create(stream1.DataString, TEncoding.UTF8);
{把第二個流的十六進位編碼顯示在 Memo 中}
bs := '';
for b in stream2.Bytes do bs := Format(bs + '%2x ', [b]);
Memo1.Lines.Add(bs);
stream1.Free;
stream2.Free;

 

function Str_Gb2UniCode(text: string): String;
var
i,len: Integer;
cur: Integer;
t: String;
ws: WideString;
begin
Result := '';
ws := text;
len := Length(ws);
i := 1;
while i <= len do
begin
cur := Ord(ws[i]);
FmtStr(t,'%4.4X',[cur]);
Result := Result + t;
Inc(i);
end;
end;


function Unicode_str(text: string):string;
var
i,len: Integer;
ws: WideString;
begin
ws := '';
i := 1;
len := Length(text);
while i < len do
begin
ws := ws + Widechar(StrToInt('$' + Copy(text,i,4)));
i := i+4;
end;
Result := ws;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(Str_Gb2UniCode('ÍòÒ»')); //4E074E00
ShowMessage(Unicode_str('4E074E00')); //ÍòÒ»
end;


Unicode和字元相互轉化的函數
2009-11-09 14:52
// 將字元轉化成Unicode 
function AnsiToUnicode(Ansi: string):string; 
var 
s:string; 
i:integer; 
j,k:string[2]; 
a:array [1..1000] of char; 
begin 
s:=''; 
StringToWideChar(Ansi,@(a[1]),500); 
i:=1; 
while ((a[i]<>#0) or (a[i+1]<>#0)) do begin 
j:=IntToHex(Integer(a[i]),2); 
k:=IntToHex(Integer(a[i+1]),2); 
s:=s+k+j; 
i:=i+2; 
end; 
Result:=s; 
end;

// 將Unicode轉化成字元 
function ReadHex(AString:string):integer; 
begin 
Result:=StrToInt('$'+AString) 
end;

function UnicodeToAnsi(Unicode: string):string; 
var 
s:string; 
i:integer; 
j,k:string[2]; 
begin 
i:=1; 
s:=''; 
while i< if end; s:="s+Char(ReadHex(j))+Char(ReadHex(k));" i:="i+4;" k:="Copy(Unicode,i,2);" j:="Copy(Unicode,i+2,2);" begin do>'' then
s:=WideCharToString(PWideChar(s+#0#0#0#0)) 
else 
s:=''; 
Result:=s; 
end;


//WideChar 相容了 AnsiChar 的 #0..#255; 但佔用了 2 位元組大小
//UniCode 字元 WideChar; 和 AnsiChar 不同, WideChar 是占 2 位元組大小.
var
c: WideChar; {WideChar 的取值範圍是: #0..#65535, 用十六進位表示是: #$0..#$FFFF}
begin
{WideChar 相容了 AnsiChar 的 #0..#255; 但佔用了 2 位元組大小}
c := #65;
ShowMessage(c); {A}
ShowMessage(IntToStr(Length(c))); {1; 這是字元長度}
ShowMessage(IntToStr(SizeOf(c))); {2; 但佔用 2 個位元組}

Navigation: 問答 >

漢字與多位元組編碼的轉換


漢字與多位元組編碼的轉換 - 回複 "不知道" 的問題

問題來源:

TEncoding.Default碼(中的16位 CE D2 C3 C7 )如何轉成漢字呢?

漢字為'我們';

--------------------------------------------------------------------------------
Delphi 2009 預設的編碼是多位元組編碼(MBCS), Delphi 這樣表示它: TEncoding.Default.

下面是多位元組編碼與漢字之間轉換的例子:

--------------------------------------------------------------------------------

 


{漢字到多位元組編碼}

procedure TForm1.Button1Click(Sender: TObject);

var

stream: TStringStream;

b: Byte;

string;

begin

stream := TStringStream.Create('我們', TEncoding.Default);
s := '';
for b in stream.Bytes do s := Format('%s%x '
ShowMessage(s); {CE D2 C3 C7}
stream.Free;

end;

{多位元組編碼到漢字}

procedure TForm1.Button2Click(Sender: TObject);
var
stream: TStringStream;
begin

stream := TStringStream.Create;
stream.Size := 4;
stream.Bytes[0] := $CE; 
stream.Bytes[1] := $D2;
stream.Bytes[2] := $C3;
stream.Bytes[3] := $C7;
ShowMessage(stream.DataString); 
stream.Free;

end;

 

{把多位元組編碼的字串轉換到漢字}

procedure TForm1.Button3Click(Sender: TObject);
var
stream: TStringStream;
i: Integer;
begin
str := 'CED2C3C7';
stream := TStringStream.Create;
stream.Size := Length(str) div 2;
for i := 1to Length(str) do
if Odd(i) then stream.Bytes[i div 2] := StrToIntDef(Concat(#36,str[i],str[i+1]), 0);
ShowMessage(stream.DataString); {我們}
stream.Free;
end;
end.


擷取所有漢字與 Unicode 的對照表 
var
w: WideString;
i: Integer;
s: string;
List: TStringList;
begin
List := TStringList.Create;
for i := $4e00 to $9fa5 do
begin
s := #36 + IntToHex(i,4); {#36 是 $ 字元}
w := WideChar(i);
List.Add(s + '='
end;

List.SaveToFile('c:\temp\Unicode-Hz.txt');
List.Free;
end;

漢字與 Unicode 轉換 
{感謝 robin(xuebin418@163.com)提供}

//轉換

functionstring): String; 
var 
i,len: Integer;
cur: Integer;
t: String;
ws: WideString;
begin
Result := '';
ws := text;
len := Length(ws);
i := 1
while i <= len do
begin
cur := Ord(ws[i]);
FmtStr(t,'%4.4X',[cur]);
Result := Result + t;
Inc(i);
end;

end;

 

//恢複
Unicode_str(text: string):string;
var
i,len: Integer;
ws: WideString;
begin
ws := '';
i := 1;
len := Length(text);
while i < len do
begin
ws := ws + Widechar(StrToInt('$' + Copy(text,i,4)));
i := i+4;
end;
Result := ws;
end;

 

//測試
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(Str_Gb2UniCode('萬一')); //4E074E00
ShowMessage(Unicode_str('4E074E00')); //萬一
end;

Windows API 中的字串對應這 Delphi 的 PChar(PAnsiChar); 在 API 中使用 Delphi 的字串還是比較靈活的.
定長字串不是 #0 結束的, 和 API 不好相容, 一般不用於 API 中. 
//賦值方法1: 給直接量

begin
SetWindowText(Handle, '新標題');
end;

--------------------------------------------------------------------------------

 

//賦值方法2: 定義它要的類型
var
p: PChar;
begin
p := '新標題';
SetWindowText(Handle, p);
end;

--------------------------------------------------------------------------------

 

//賦值方法3: 轉換成它要的類型
var
str: string;
begin
str := '新標題';
SetWindowText(Handle, PChar(str));
end;

//賦值方法4: 用字元數組
var
arr: array[0..255] of Char;
begin
arr := '新標題';
SetWindowText(Handle, arr);
end;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.