解决Delphi7的自带的UTF-8编码转换函数BUG
生活随笔
收集整理的這篇文章主要介紹了
解决Delphi7的自带的UTF-8编码转换函数BUG
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Delphi7及其以下版本的 VCL 只支持 Ansi, 所以... WideString 與 UTF8String (定義與 AnsiString 相同) 并沒有辦法正確的在 VCL 中顯示
Delphi7自帶的utf-8轉換函數遇到其無法識別的字符串就返回空。
用以下轉換函數可以解決這個bug
unit util_utf8;interfaceuses Windows;typeUTF8String = AnsiString;function AnsiToWide(const S: AnsiString): WideString;function WideToUTF8(const WS: WideString): UTF8String;function AnsiToUTF8(const S: AnsiString): UTF8String;function UTF8ToWide(const US: UTF8String): WideString;function WideToAnsi(const WS: WideString): AnsiString;function UTF8ToAnsi(const S: UTF8String): AnsiString;implementationfunction AnsiToWide(const S: 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;function WideToUTF8(const WS: 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;function AnsiToUTF8(const S: AnsiString): UTF8String; beginResult:=WideToUTF8(AnsiToWide(S)); end;function UTF8ToWide(const US: 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;function WideToAnsi(const WS: 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;function UTF8ToAnsi(const S: UTF8String): AnsiString; beginResult:=WideToAnsi(UTF8ToWide(S)); end;end.?
unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,IdTCPClient, IdHTTP;typeTForm1 = class(TForm)Button1: TButton;Memo1: TMemo;IdHTTP1: TIdHTTP;procedure Button1Click(Sender: TObject);private{ Private declarations }public{ Public declarations }end;varForm1: TForm1;implementation usesutil_utf8; {$R *.dfm}procedure TForm1.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.窗體文件
object Form1: TForm1Left = 206Top = 211Width = 783Height = 540Caption = 'Form1'Color = clBtnFaceFont.Charset = DEFAULT_CHARSETFont.Color = clWindowTextFont.Height = -11Font.Name = 'MS Sans Serif'Font.Style = []OldCreateOrder = FalsePixelsPerInch = 96TextHeight = 13object Button1: TButtonLeft = 56Top = 40Width = 75Height = 25Caption = 'Button1'TabOrder = 0OnClick = Button1Clickendobject Memo1: TMemoLeft = 24Top = 88Width = 737Height = 409Lines.Strings = ('Memo1')ScrollBars = ssVerticalTabOrder = 1endobject IdHTTP1: TIdHTTPMaxLineAction = maExceptionReadTimeout = 0AllowCookies = TrueProxyParams.BasicAuthentication = FalseProxyParams.ProxyPort = 0Request.ContentLength = -1Request.ContentRangeEnd = 0Request.ContentRangeStart = 0Request.ContentType = 'text/html'Request.Accept = 'text/html, */*'Request.BasicAuthentication = FalseRequest.UserAgent = 'Mozilla/3.0 (compatible; Indy Library)'HTTPOptions = [hoForceEncodeParams]Left = 240Top = 48end end
測試效果:
?
總結
以上是生活随笔為你收集整理的解决Delphi7的自带的UTF-8编码转换函数BUG的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: zg手册 之 python2.7.7源码
- 下一篇: Android UI开发第一篇——and