解决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相关推荐
- html转换编码格式,html编码转换 html编码设置utf gbk编码转换图文教程
html编码转换 html编码设置utf gbk编码转换图文教程篇 常用HTML编码之urf-8编码转换为gb2312编码或者gb2312转换为utf-8编码快速转换设置,这里DIVCSS5介绍使用D ...
- 解决数据库里表字段带下划线,实体类转小驼峰,Mapper的映射问题
解决数据库里表字段带下划线,实体类转小驼峰,Mapper的映射问题 参考文章: (1)解决数据库里表字段带下划线,实体类转小驼峰,Mapper的映射问题 (2)https://www.cnblogs. ...
- Linux(shell)遍历目录删除指定文件,解决文件夹名称带空格问题
最近打算把一些学习资源解压上传到阿里网盘上,可是在解压的时候出现了问题,无故产生很多隐藏文件(猪哥mac电脑,用的keka解压软件). 且文件夹层级很多,不可能实现手动一个一个文件夹删除,所以就考虑写 ...
- 解决ios设备自带邮箱只能接收邮件不能发送邮件问题的方法
解决ios设备自带邮箱只能接收邮件不能发送邮件问题的方法 解决的办法:邮件\通讯录\日历 一: (1)用手机网页登录QQ邮箱网页版 (2)登录后,拉到下面,设置-账户,选择这个号码的作为默认发信帐号– ...
- python utf—8编码问题待解决
执行python manage. py runserver后出现错误 utf-8编码出错 (result, consumed) = self._buffer_encode(data, self.err ...
- C语言 realloc函数 带着内存游走的函数
realloc函数 realloc函数,带着内存游走的函数. 该函数将 ptr 指向的内存大小修改为size.内存中的数据从开头到size保留不变. 如果size大于原内存大小,则多出的部分会被初始化 ...
- 前端将后端返回的带颜色的数据进行转换
前端将后端返回的带颜色的数据进行转换 因为之前在调用后端接口的时候 后端返给我的数据显示乱码:具体如下图所示: 然后这些数据 在后台显示的是这样的的,如下图所示: 根据以上这三幅图片可以看到有些数据 ...
- 项目总结10:通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题...
通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题 关键字 springboot热部署 ClassCastException异常 反射 ...
- VC编写自己构造http协议数据的post上传图片类(MFC环境 带编码转换)(转)
上次用VC写了个MFC环境下的post数据类,地址: /Html/diannaojishu/2009-10/4905858580.html 这次将这个类进行扩展,可以post图片.但这两个post所使 ...
最新文章
- 明早10点大讲堂 | 一种新型光幕传感器在机器人避障和无人驾驶中的应用
- java Character类的一些简单的方法
- ubuntu下编译安装PHP
- XWiki 11.1 发布,协作式应用开发平台
- Freemaker FTL指令常用标签及语法
- switch里能不能用continue?
- smart原则_为什么现在少有人用德鲁克的SMART原则做目标管理了?
- RFE筛选出的特征变量竟然是Boruta的4倍之多
- 即插即用!开源项目【云框架】发布“基于Spring cloud的微服务架构”
- LeetCode 70.爬楼梯(动态规划)
- 图书管理系统实验报告-面向对象的分析与设计
- C++ 的万能头文件,你知道多少?
- Fedora下安装Nerolinux 4刻录软件
- DocumentBuilderFactory.newInstance() 异常解决
- gis统计百分比_详细讲解ArcGIS数据统计及字段计算
- 马赛克,一生之敌,是时候说再见了【兄弟,借一部说话】
- SQL分组选取时间最大的记录
- 为什么招聘单位都要求你有ITIL证书,你知道吗?
- 水晶五笔下载地址(非技术)
- android 调试原理