unit util_utf8;
  
interface
  
uses Windows;
  
type
  UTF8String = 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;
  
implementation
  
function AnsiToWide(const S: AnsiString): WideString;
var
  len: integer;
  ws: WideString;
begin
  Result:='';
  if (Length(S) = ) then
    exit;
  len:=MultiByteToWideChar(CP_ACP, , PChar(s), -, nil, );
  SetLength(ws, len);
  MultiByteToWideChar(CP_ACP, , PChar(s), -, PWideChar(ws), len);
  Result:=ws;
end;
  
function WideToUTF8(const WS: WideString): UTF8String;
var
  len: integer;
  us: UTF8String;
begin
  Result:='';
  if (Length(WS) = ) then
    exit;
  len:=WideCharToMultiByte(CP_UTF8, , PWideChar(WS), -, nil, , nil, nil);
  SetLength(us, len);
  WideCharToMultiByte(CP_UTF8, , PWideChar(WS), -, PChar(us), len, nil, nil);
  Result:=us;
end;
  
function AnsiToUTF8(const S: AnsiString): UTF8String;
begin
  Result:=WideToUTF8(AnsiToWide(S));
end;
  
function UTF8ToWide(const US: UTF8String): WideString;
var
  len: integer;
  ws: WideString;
begin
  Result:='';
  if (Length(US) = ) then
    exit;
  len:=MultiByteToWideChar(CP_UTF8, , PChar(US), -, nil, );
  SetLength(ws, len);
  MultiByteToWideChar(CP_UTF8, , PChar(US), -, PWideChar(ws), len);
  Result:=ws;
end;
  
function WideToAnsi(const WS: WideString): AnsiString;
var
  len: integer;
  s: AnsiString;
begin
  Result:='';
  if (Length(WS) = ) then
    exit;
  len:=WideCharToMultiByte(CP_ACP, , PWideChar(WS), -, nil, , nil, nil);
  SetLength(s, len);
  WideCharToMultiByte(CP_ACP, , PWideChar(WS), -, PChar(s), len, nil, nil);
  Result:=s;
end;
  
function UTF8ToAnsi(const S: UTF8String): AnsiString;
begin
  Result:=WideToAnsi(UTF8ToWide(S));
end;
  
end.
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdHTTP;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    IdHTTP1: TIdHTTP;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
uses
  util_utf8;
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
var
  strm: TStringStream;
begin
  strm := TStringStream.Create('');
  try
    IdHTTP1.Get('http://gz.ganji.com/zpshichangyingxiao/', strm);
    Memo1.Clear;
    Memo1.Lines.Add(UTF8ToAnsi(strm.DataString));
  finally
    strm.Free;
  end;
end;
 
end.

UTF-8 delphi 函数的更多相关文章

  1. 转:Delphi 函数大全

    Delphi 函数大全 - xiucaiyao的专栏 - 博客频道 - CSDN.NEThttp://blog.csdn.net/xiucaiyao/article/details/4544039 名 ...

  2. cef3 获得js 返回值, 以及js 指挥delphi 函数的 总结参考

     cef3  如何加载 本地html 文件.   请教老师[吐槽]常忘<run_fan@qq.com>  22:21:45@lazarus 下载cef3中的范例中就有  [吐槽]常忘< ...

  3. Delphi 函数指针(三大好处:灵活,委托的本质,回调机制),还可把函数指针当参数传入

    首先学习: 指向非对象(一般的)函数/过程的函数指针 Pascal 中的过程类型与C语言中的函数指针相似,为了统一说法,以下称函数指针.函数指针的声明只需要参数列表:如果是函数,再加个返回值.例如声明 ...

  4. Delphi函数的out、var等关键字的作用,和使用场景

    问题描述 Delphi函数的out.var等关键字的作用,和使用场景 Delphi函数的out.var等关键字的作用,和使用场景,我知道var是作为传值调用,但是像out这个关键字又是什么作用呢? 解 ...

  5. delphi函数大全

    delphi函数大全Abort                 函数    引起放弃的意外处理Abs                   函数    绝对值函数AddExitProc          ...

  6. delphi 函数isiconic 函数 判断窗口是否最小化

    http://blog.sina.com.cn/s/blog_66357ab901012t2h.html delphi 函数isiconic 函数 判断窗口是否最小化 (2012-05-26 22:0 ...

  7. Delphi函数的返回值(注意这里与C/C++等语言有差异)

    在C/C++等语言中,函数执行到 return 部分之后,将立即停止函数的执行,并返回值 但是在Delphi中不同 函数中,执行到result时,并不同于比如 C/C++ 中的 return,跳出函数 ...

  8. Delphi函数参数传递 默认参数(传值)、var(穿址)、out(输出)、const(常数)四类

    Delphi的参数可以分为:默认参数(传值).var(传址).out(输出).const(常数)四类 可以对比C/C++的相关知识,类比学习. 1.默认参数是传值,不会被改变,例子 function ...

  9. Delphi函数指针

    参考:http://blog.chinaunix.net/uid-91034-id-2009700.html http://blog.csdn.net/procedure1984/article/de ...

  10. Delphi函数翻译成VC要注意句柄指针传递(传递Handle的时候,必须加上一个指针引用,才能消除编译错误)

    Delphi里做了魔法变化,每个变量名称本身就是指针,因为不怎么需要指针语法.我也不知道是不是因为这个原因引起的Delphi与VC对句柄的不同处理. 这是Delphi的强行关机函数,好用,调用方式:W ...

随机推荐

  1. Android注解支持(Support Annotations) (转)

    原文地址:http://www.flysnow.org/2015/08/13/android-tech-docs-support-annotations.html 注解支持(Support Annot ...

  2. 第6章 网页解析器和BeautifulSoup第三方插件

    第一节 网页解析器简介作用:从网页中提取有价值数据的工具python有哪几种网页解析器?其实就是解析HTML页面正则表达式:模糊匹配结构化解析-DOM树:html.parserBeautiful So ...

  3. (总结)RHEL/CentOS 7.x的几点新改变

    一.CentOS的Services使用了systemd来代替sysvinit管理 1.systemd的服务管理程序: systemctl是主要的工具,它融合之前service和chkconfig的功能 ...

  4. Android 热门技术干货

    http://mp.weixin.qq.com/s?__biz=MzIwMzYwMTk1NA==&mid=2247484939&idx=1&sn=d1871b09de55ca6 ...

  5. ASIHTTPRequest-Cookie的使用

    本文转载至 http://www.cocoachina.com/bbs/read.php?tid=93220&page=e&#a     持久化cookie ASIHTTPReques ...

  6. 实现asp.net mvc页面二级缓存,提高访问性能

    实现的mvc二级缓存的类 //Asp.Net MVC视图页面二级缓存 public class TwoLevelViewCache : IViewLocationCache { private rea ...

  7. python login form

    import time from selenium import webdriver browser = webdriver.Chrome() wait_time = 1 USER = 'xl.fen ...

  8. sin6_addr打印:string to sockaddr_in6 and sockaddr_in6 to string

    函式原型: #include <arpa/inet.h> const char *inet_ntop(int af, const void *src, char *dst, socklen ...

  9. 在网页中显示PDF文件及vue项目中弹出PDF

    1.<embed width="800" height="600" src="test_pdf.pdf"> </embed ...

  10. spring ioc和aop理解

    1.IOC 表示控制反转. 简单点说就是原来的对象是在要使用之前通过在代码里通过new Something()的方式创建出来的: IOC则是由spring容器创建同一创建,在程序要使用到该对象的时候, ...