TEncoding and TNetEncoding are abstract classes and you will never instantiate one of them, because only the descendants will have the full functionality. Normally you would need to write something like this...

Code:
var
Encoding: TEncoding
Buffer: TArray<Byte>;
begin
Encoding := TMBCSEncoding.Create(GetACP, 0, 0);
try
Buffer := Encoding.GetBytes('Hello world!');
(...)
finally
Encoding.Free();
end;
end;

...and you get the ANSI values for "Hello world!". But this isn't really funny and bloates the code, therefore often used Encodings like ASCII, ANSI, UTF8 and other are implemented as Singleton. Therefore the using is normally something like this...

Code:
var
Buffer: TArray<Byte>;
begin
Buffer := TEncoding.ANSI.GetBytes('Hello world!');
(...)
end;

In this case the RTL holds and manages an instance of an ANSI encoding object and you will always get the same object. Now sometimes it's needed to work with other code pages and for this cases the function "GetEncoding(...)" exists. You will use it in this way...

Code:
var
Encoding: TEncoding;
Buffer: TArray<Byte>;
begin
Encoding := TEncoding.GetEncoding(852);
try
Buffer := Encoding.GetBytes('Hello');
(...)
finally
Encoding.Free(); <- must have
end;
end;

Note: You need to destroy such instances by your own. In case you use often the same code page, just use a global variable, otherwise use it like any other class too.

The class TNetEncoding has the same schema as TEncoding is using. It is an abstract class which offers with the properties Base64, HTML and URL certain encoder. Even the class TBase64Encoding has an overloaded constructor, which allows to tweak the output. In the example below is the "LineSeparator" empty and therefore the output has not line breaks, which is far away from what default behaviour offers.

Code:
class function TEncodingUtils.BytesToBase64(Buffer: PByte; const Offset, Count: Integer): string;
var
Encoder: TBase64Encoding;
Input: PByte;
begin
Encoder := TBase64Encoding.Create(MaxInt, '');
try
Input := Buffer;
Inc(Input, Offset);
Result := Encoder.EncodeBytesToString(Input, Count);
finally
Encoder.Free();
end;
end;

https://www.board4allcz.eu/showthread.php?t=634856

TEncoding & TNetEncoding(使用现成的TBase64Encoding,TEncoding和TMBCSEncoding)的更多相关文章

  1. delphi TEncoding

    #include <tchar.h> #include <memory> //For STL auto_ptr class //------------------------ ...

  2. kbmmw 做REST 服务签名认证的一种方式

    一般对外提供提供REST 服务,由于信息安全的问题, 都要采用签名认证,今天简单说一下在KBMMW 中如何 实现简单的签名服务? 整个签名服务,模仿阿里大鱼的认证方式,大家可以根据实际情况自己修改. ...

  3. c++Builder XE6 MD5 加密算法 BASE64 URL 编码

    xe6,xe7 BASE64XE6 MD5 加密算法Delphifunction MD5(const texto: string): string; var idmd5: TIdHashMessage ...

  4. TNetHTTPClient演示

    TNetHTTPClient演示 TNetHTTPClient是DELPHI新增加的异步HTTP通信控件(区别于INDY的阻塞控件). unit Unit1; interface uses Winap ...

  5. TNetHTTPClient 使用

    unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...

  6. TNetHttpClient的用法

    TNetHttpClient的用法 TNetHttpClient是DELPHI XE8新增加的控件. 在之前,我们一般都是使用IDHTTP控件,但在安卓.IOS等非WINDOWS平台,IDHTTP访问 ...

  7. delphi中webbrowser的用法

    WebBrowser1.GoHome; //到浏览器默认主页 WebBrowser1.Refresh; //刷新 WebBrowser1.GoBack; //后退 WebBrowser1.GoForw ...

  8. Delphi WebBrowser控件的使用(大全 good)

    Delphi WebBrowser控件的使用 WebBrowser控件属性:1.Application      如果该对象有效,则返回掌管WebBrowser控件的应用程序实现的自动化对象(IDis ...

  9. Delphi TWebBrowser

    Delphi WebBrowser控件的使用 WebBrowser控件属性:1.Application      如果该对象有效,则返回掌管WebBrowser控件的应用程序实现的自动化对象(IDis ...

随机推荐

  1. 【转】android UI进阶之实现listview中checkbox的多选与记录--不错

    原文网址:http://www.cnblogs.com/notice520/archive/2012/02/17/2355415.html 今天继续和大家分享涉及到listview的内容.在很多时候, ...

  2. HDU_2028——求多个数的最小公倍数

    Problem Description 求n个数的最小公倍数.   Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数.   Output 为每组测试数据输出它们的最 ...

  3. zoj3658 Simple Function (函数值域)

    Simple Function Time Limit: 2 Seconds       Memory Limit: 32768 KB Knowing that x can be any real nu ...

  4. SQL Server 各任务所维护

    SQL Server 正在运行的代码查看 SELECT [Spid] = session_id , ecid , [Database] = DB_NAME(sp.dbid) , [User] = nt ...

  5. IT江湖

    我喜欢看武侠电影,尤其的70-80年代的邵氏电影,在这个期间,邵氏公司将金庸和古老很多小说都改拍成了电影,可以说,看这些电影是一种享受,真的! 对于现实中的IT世界,也像是一个江湖,当你掌握了一些技能 ...

  6. c++ THUNK技术

    这里想说的是:代码中的关键点为用指令jmp pFunc跳转到你想要运行的函数pFunc. 指令"jmp xxxx"占5个字节,代码中用了个一字节对齐的结构体struct Thunk ...

  7. UI Prototype Design IDE( 界面原型设计工具 )

    UI Prototype Design IDE( 界面原型设计工具 )   如何用工具去与客户进行交流,互动,定义要做的系统,什么什么的... 0.Balsamiq Mockups http://ww ...

  8. 使用Mina框架开发 QQ Android 客户端

    Apache MINA是一个网络应用程序框架,用来帮助用户简单地开发高性能和高可靠性的网络应用程序.它提供了一个通过Java NIO在不同的传输例如TCP/IP和UDP/IP上抽象的事件驱动的异步AP ...

  9. C# Socket传输大文件

    1.基础类TransferFiles,client和server都需要 using System; using System.Collections.Generic; using System.Tex ...

  10. linux添加JAVA环境变量

    root用户: 1.修改文件vim /etc/profile 添加以下信息: export JAVA_HOME=/home/jdk1..0_79 (这里需要添加自己的JDK安装目录) export C ...