TEncoding & TNetEncoding(使用现成的TBase64Encoding,TEncoding和TMBCSEncoding)
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...
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...
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...
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.
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)的更多相关文章
- delphi TEncoding
#include <tchar.h> #include <memory> //For STL auto_ptr class //------------------------ ...
- kbmmw 做REST 服务签名认证的一种方式
一般对外提供提供REST 服务,由于信息安全的问题, 都要采用签名认证,今天简单说一下在KBMMW 中如何 实现简单的签名服务? 整个签名服务,模仿阿里大鱼的认证方式,大家可以根据实际情况自己修改. ...
- c++Builder XE6 MD5 加密算法 BASE64 URL 编码
xe6,xe7 BASE64XE6 MD5 加密算法Delphifunction MD5(const texto: string): string; var idmd5: TIdHashMessage ...
- TNetHTTPClient演示
TNetHTTPClient演示 TNetHTTPClient是DELPHI新增加的异步HTTP通信控件(区别于INDY的阻塞控件). unit Unit1; interface uses Winap ...
- TNetHTTPClient 使用
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- TNetHttpClient的用法
TNetHttpClient的用法 TNetHttpClient是DELPHI XE8新增加的控件. 在之前,我们一般都是使用IDHTTP控件,但在安卓.IOS等非WINDOWS平台,IDHTTP访问 ...
- delphi中webbrowser的用法
WebBrowser1.GoHome; //到浏览器默认主页 WebBrowser1.Refresh; //刷新 WebBrowser1.GoBack; //后退 WebBrowser1.GoForw ...
- Delphi WebBrowser控件的使用(大全 good)
Delphi WebBrowser控件的使用 WebBrowser控件属性:1.Application 如果该对象有效,则返回掌管WebBrowser控件的应用程序实现的自动化对象(IDis ...
- Delphi TWebBrowser
Delphi WebBrowser控件的使用 WebBrowser控件属性:1.Application 如果该对象有效,则返回掌管WebBrowser控件的应用程序实现的自动化对象(IDis ...
随机推荐
- 【转】Android:ListView常见错位之CheckBox错位
原文网址:http://blog.csdn.net/lemon_tree12138/article/details/39337867 ListView在什么样的情况下会出现错位?错位的原因是什么?怎么 ...
- 51nod-正整数分组问题(基础方程DP-01背包)
正整数分组 将一堆正整数分为2组,要求2组的和相差最小. 例如:1 2 3 4 5,将1 2 4分为1组,3 5分为1组,两组和相差1,是所有方案中相差最少的. 思路: 这题的实质其实也是0-1背包问 ...
- [饭后算法系列] 数组中"和非负"的最长子数组
1. 问题 给定一列数字数组 a[n], 求这个数组中最长的 "和>=0" 的子数组. (注: "子数组"表示下标必须是连续的. 另一个概念"子 ...
- 【转】在ASP.NET MVC中,使用Bundle来打包压缩js和css
在ASP.NET MVC4中(在WebForm中应该也有),有一个叫做Bundle的东西,它用来将js和css进行压缩(多个文件可以打包成一个文件),并且可以区分调试和非调试,在调试时不进行压缩,以原 ...
- LINQ 用法,返回结果不是在定义时取值,而是在调用时实时取值,有意思!
var names = new List<string> { "Nino o", "Alberto", "Juan", &quo ...
- [原创]Python入门到简单网站目录扫描器编写(上)
1.字符串,整型,浮点型.区别以及用法 |------字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 可以不严格的说,你可以认为引号包括的,都属于字符串 ...
- __device__ __global__ __host__
__device__ 标记的函数从一个在器件中执行的函数呼叫,在器件中执行 __global__ 表示该函数从一个在主机中执行的函数呼叫,在器件中执行 __host__表示在主机中呼叫,在主机中执行的 ...
- Linux安装WebLogic12
# groupadd weblogic# useradd -g weblogic weblogic# passwd weblogic# mkdir -p /var/bea# chown -R webl ...
- Python之基础(二)
1.内建函数enumerate friends = ['john', 'pat', 'gary', 'michael'] for i, name in enumerate(friends): prin ...
- 【转载】Python编程中常用的12种基础知识总结
Python编程中常用的12种基础知识总结:正则表达式替换,遍历目录方法,列表按列排序.去重,字典排序,字典.列表.字符串互转,时间对象操作,命令行参数解析(getopt),print 格式化输出,进 ...