UnitUtil_utf8; Interface usesWindows; typeutf8string=ansistring; functionAnsitowide (Consts:ansistring): widestring; functionWideToUTF8 (Constws:widestring): utf8string; functionAnsiToUTF8 (Consts:ansistring): utf8string; functionUtf8towide (Constus:utf8string): widestring; functionWidetoansi (Constws:widestring): ansistring; functionUtf8toansi (Consts:utf8string): ansistring; Implementation functionAnsitowide (Consts:ansistring): widestring;varLen:integer; ws:widestring;beginResult:="'; if(Length (S) =0) Thenexit; Len:=multibytetowidechar (CP_ACP,0, PChar (s),-1,Nil,0); SetLength (WS, Len); MultiByteToWideChar (CP_ACP,0, PChar (s),-1, Pwidechar (WS), Len); Result:=ws;End; functionWideToUTF8 (Constws:widestring): utf8string;varLen:integer; us:utf8string;beginResult:="'; if(Length (WS) =0) Thenexit; Len:=widechartomultibyte (Cp_utf8,0, Pwidechar (WS),-1,Nil,0,Nil,Nil); SetLength (US, Len); WideCharToMultiByte (Cp_utf8,0, Pwidechar (WS),-1, PChar (US), Len,Nil,Nil); Result:=us;End; functionAnsiToUTF8 (Consts:ansistring): utf8string;beginResult:=WideToUTF8 (Ansitowide (S));End; functionUtf8towide (Constus:utf8string): widestring;varLen:integer; ws:widestring;beginResult:="'; if(Length (US) =0) Thenexit; Len:=multibytetowidechar (Cp_utf8,0, PChar (US),-1,Nil,0); SetLength (WS, Len); MultiByteToWideChar (Cp_utf8,0, PChar (US),-1, Pwidechar (WS), Len); Result:=ws;End; functionWidetoansi (Constws:widestring): ansistring;varLen:integer; s:ansistring;beginResult:="'; if(Length (WS) =0) Thenexit; Len:=widechartomultibyte (CP_ACP,0, Pwidechar (WS),-1,Nil,0,Nil,Nil); SetLength (S, Len); WideCharToMultiByte (CP_ACP,0, Pwidechar (WS),-1, PChar (s), Len,Nil,Nil); Result:=s;End; functionUtf8toansi (Consts:utf8string): ansistring;beginResult:=Widetoansi (Utf8towide (S));End; End.
UnitUnit1;Interface usesWindows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls, Idbasecomponent, IdC Omponent, Idtcpconnection, Idtcpclient, idhttp; typeTForm1=class(tform) Button1:tbutton; Memo1:tmemo; Idhttp1:tidhttp; procedureButton1Click (Sender:tobject); Private {Private Declarations} Public {Public Declarations} End; varForm1:tform1;ImplementationusesUtil_utf8;{$R *.DFM} procedureTform1.button1click (sender:tobject);varStrm:tstringstream;beginSTRM:= Tstringstream.Create("'); TryIdhttp1.get ('http://gz.ganji.com/zpshichangyingxiao/', STRM); Memo1.clear; MEMO1.LINES.ADD (Utf8toansi (STRM. datastring)); finallySTRM. Free; End;End; End.
UTF-8 Delphi function