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. javascript变量:全局?还是局部?这个得注意!

    做项目就是一个学习的过程! 做为一个学习C3年多,从C程序员转前端的人来说,javascript中的许多规则是没办法或者说一时半会理解不了的. 今天就遇到了一个,大致就是这么个代码. var a ; ...

  2. POJ3581---Sequence 后缀树组

    题意:n个数字组成的序列,第一个数字最大,,把序列分成3部分,每个部分分别翻转,输出翻转后字典序最小的序列.. 后缀数组变一下,,先求出 第一个分割的位置,,然后再求一次后缀数组,,求出第二个位置.. ...

  3. HDOJ 2191

    多重背包. 模版. #include <iostream> #include <stdio.h> #include <stdlib.h> #include < ...

  4. 【转】Mac访问Windows共享文件夹

    相信大多数的用户用Windows访问Windows的共享文件夹是一件很容易的事,但是如果用Mac来访问Windows共享文件夹就会遇到很多的麻烦了,尤其是设置是比较有区别的吗,接下来的将用图文交大家怎 ...

  5. markdown转dokuwiki

    markdown markdown格式变得越来越通用,很多博客,云笔记,github等等都支持markdown编写,markdown也能很方便的转化为pdf,html等格式.公司的文档用的dokuwi ...

  6. iOS BMOB-登录注册手机验证码、邮箱验证

    1.下载BmobSDK 2.把里面的BmobSDK.framework拖到你的工程里面的工具文件夹内. 3.导入类库 CoreLocation.framework.Security.framework ...

  7. jquery中ajax的用法

    Jquery中队Ajax操作进行了封装,可分为3层:1.最底层$.ajax(),2.第二层load().$.get().$.post()方法,3.第三层$.getScript()和$.getJSON( ...

  8. mysql数据库优化[千万级查询]

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  9. spring整合springMVC、mybatis、hibernate、mongodb框架

    开发环境 eclipse Mars 4.5 JDK 1.7 框架 spring 4.0.5 mybatis 3.2.7 hibernate 4.3.6 mongodb 1.7 数据库 MySQL 5. ...

  10. WIN7局域网文件共享设置方法

    WIN7局域网文件共享设置方法 工具/原料 两台电脑以上的局域网.WIN7操作系统 步骤/方法 1 右击桌面网络----属性----更改高级共享设置 (注释:查看当前网络 比如:家庭网络.公共网络 等 ...