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. 我学hash_map(2)

    啊,转眼之间就来到了我学hash_map(2)了.我们也从hash_map转移到了unordered_map上来了,今天这个文章的目的就是要来分享一下使用这个hash_map,哦不,unordered ...

  2. STL_set&multiset

    1,set的含义是集合,它是一个有序的容器,里面的元素都是排序好的,支持插入,删除,查找等操作,就 像一个集合一样.所有的操作的都是严格在logn时间之内完成,效率非常高. set和multiset的 ...

  3. HDU_2043——判断密码是否安全

    Problem Description 网上流传一句话:"常在网上飘啊,哪能不挨刀啊-".其实要想能安安心心地上网其实也不难,学点安全知识就可以.首先,我们就要设置一个安全的密码. ...

  4. Java[2] 分布式服务架构之java远程调用技术浅析(转http://www.uml.org.cn/zjjs/201208011.asp)

    转自:http://www.uml.org.cn/zjjs/201208011.asp 在分布式服务框架中,一个最基础的问题就是远程服务是怎么通讯的,在Java领域中有很多可实现远程通讯的技术,例如: ...

  5. css链接

    css code: a:link{ color:#FF0000; } a:visited{ color:#00FF00; } a:hover { color:#0000FF; } a:active{ ...

  6. appium新版本不支持findElementByName,切换到findElementByAndroidUIAutomator

    appium 1.7.6 不支持findElementByName(locator)  不知道为什么? 脚本中许多这样的语句,麻烦事情多了 org.openqa.selenium.InvalidSel ...

  7. SQL Server 启用 xp_cmdshell 与bcp 使用

    启用 xp_cmdshell 1: sp_configure 'show advanced options',1 2: reconfigure 3: GO 4: 5: sp_configure 'xp ...

  8. [教程] 神器i9100刷基带与内核的方法!(兼带ROOT方法)

    http://bbs.hiapk.com/thread-2647905-1-1.html ------何为基带?何为内核? 为什么刷基带,为什么刷内核?!!! 基带:基带(Baseband)是手机中的 ...

  9. nexus4/5/6/7/9/10设备谷歌安卓5.1.1系统底包下载

    https://developers.google.com/android/nexus/images http://www.inexus.co/thread-18488-1-1.html

  10. openfire连接登陆优化方案

    client登陆openfire,大概总共须要9个来回才完毕登录. 在2G情况下.就表现为client登录特别慢,所以,为解决问题,对openfire进行了例如以下优化 openfire的连接.登陆过 ...