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相关推荐

  1. html转换编码格式,html编码转换 html编码设置utf gbk编码转换图文教程

    html编码转换 html编码设置utf gbk编码转换图文教程篇 常用HTML编码之urf-8编码转换为gb2312编码或者gb2312转换为utf-8编码快速转换设置,这里DIVCSS5介绍使用D ...

  2. 解决数据库里表字段带下划线,实体类转小驼峰,Mapper的映射问题

    解决数据库里表字段带下划线,实体类转小驼峰,Mapper的映射问题 参考文章: (1)解决数据库里表字段带下划线,实体类转小驼峰,Mapper的映射问题 (2)https://www.cnblogs. ...

  3. Linux(shell)遍历目录删除指定文件,解决文件夹名称带空格问题

    最近打算把一些学习资源解压上传到阿里网盘上,可是在解压的时候出现了问题,无故产生很多隐藏文件(猪哥mac电脑,用的keka解压软件). 且文件夹层级很多,不可能实现手动一个一个文件夹删除,所以就考虑写 ...

  4. 解决ios设备自带邮箱只能接收邮件不能发送邮件问题的方法

    解决ios设备自带邮箱只能接收邮件不能发送邮件问题的方法 解决的办法:邮件\通讯录\日历 一: (1)用手机网页登录QQ邮箱网页版 (2)登录后,拉到下面,设置-账户,选择这个号码的作为默认发信帐号– ...

  5. python utf—8编码问题待解决

    执行python manage. py runserver后出现错误 utf-8编码出错 (result, consumed) = self._buffer_encode(data, self.err ...

  6. C语言 realloc函数 带着内存游走的函数

    realloc函数 realloc函数,带着内存游走的函数. 该函数将 ptr 指向的内存大小修改为size.内存中的数据从开头到size保留不变. 如果size大于原内存大小,则多出的部分会被初始化 ...

  7. 前端将后端返回的带颜色的数据进行转换

    前端将后端返回的带颜色的数据进行转换 因为之前在调用后端接口的时候 后端返给我的数据显示乱码:具体如下图所示: 然后这些数据 在后台显示的是这样的的,如下图所示: 根据以上这三幅图片可以看到有些数据 ...

  8. 项目总结10:通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题...

    通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题 关键字 springboot热部署  ClassCastException异常 反射 ...

  9. VC编写自己构造http协议数据的post上传图片类(MFC环境 带编码转换)(转)

    上次用VC写了个MFC环境下的post数据类,地址: /Html/diannaojishu/2009-10/4905858580.html 这次将这个类进行扩展,可以post图片.但这两个post所使 ...

最新文章

  1. 明早10点大讲堂 | 一种新型光幕传感器在机器人避障和无人驾驶中的应用
  2. java Character类的一些简单的方法
  3. ubuntu下编译安装PHP
  4. XWiki 11.1 发布,协作式应用开发平台
  5. Freemaker FTL指令常用标签及语法
  6. switch里能不能用continue?
  7. smart原则_为什么现在少有人用德鲁克的SMART原则做目标管理了?
  8. RFE筛选出的特征变量竟然是Boruta的4倍之多
  9. 即插即用!开源项目【云框架】发布“基于Spring cloud的微服务架构”
  10. LeetCode 70.爬楼梯(动态规划)
  11. 图书管理系统实验报告-面向对象的分析与设计
  12. C++ 的万能头文件,你知道多少?
  13. Fedora下安装Nerolinux 4刻录软件
  14. DocumentBuilderFactory.newInstance() 异常解决
  15. gis统计百分比_详细讲解ArcGIS数据统计及字段计算
  16. 马赛克,一生之敌,是时候说再见了【兄弟,借一部说话】
  17. SQL分组选取时间最大的记录
  18. 为什么招聘单位都要求你有ITIL证书,你知道吗?
  19. 水晶五笔下载地址(非技术)
  20. android 调试原理

热门文章

  1. 机器人(机械臂)动力学建模方法(Newton-Euler equation)
  2. markdown常用操作(特殊字符显示、换行、字体颜色和大小、图片位置和大小)
  3. apache 与 php-fpm 几种处理方式
  4. 匹配两位小数,js正则
  5. springboot项目启动优化
  6. 【POI xls】解析xls遇到的问题
  7. C# Settings使用小结
  8. Android TableLayout 常用的属性介绍及演示
  9. rancher2.0安装mysql_高可用rancher集群搭建
  10. CSS自动换行,强迫不换行