Delphi 自带了 Base64 编解码的单元,叫 EncdDecd,
这名字很拗口而且不直观,估计这是一直很少人关注和知道的原因。  
这个单元提供两套四个公开函数:  对流的编解码: procedure EncodeStream(Input, Output: TStream); 
// 编码 procedure DecodeStream(Input, Output: TStream); // 解码  
// 对字符串的编解码: 
function  EncodeString(const Input: string): string; 
// 编码 
function  DecodeString(const Input: string): string;

// 解码  这几个函数在帮助中没有。应该不算是标准库中的函数。

{********************************************************}
{ }
{ Borland Delphi Visual Component Library }
{ }
{ Copyright (c) 2000, 2001 Borland Software Corporation }
{ } {********************************************************}
unit EncdDecd;
{ Have string use stream encoding since that logic wraps properly }
interface uses Classes;
procedure EncodeStream(Input, Output: TStream);
procedure DecodeStream(Input, Output: TStream);
function EncodeString(const Input: string): string;
function DecodeString(const Input: string): string; implementation
const
EncodeTable: array[..] of Char =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
'abcdefghijklmnopqrstuvwxyz' +
'0123456789+/';
DecodeTable: array[#..#] of Integer = (
Byte('='), , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , );
type PPacket = ^TPacket;
TPacket = packed record
case Integer of : (b0, b1, b2, b3: Byte);
: (i: Integer);
: (a: array[..] of Byte);
: (c: array[..] of Char);
end;
procedure EncodePacket(const Packet: TPacket; NumChars: Integer; OutBuf: PChar);
begin
OutBuf[] := EnCodeTable[Packet.a[] shr ];
OutBuf[] := EnCodeTable[((Packet.a[] shl ) or (Packet.a[] shr )) and $0000003f];
if NumChars < then
OutBuf[] := '='
else
OutBuf[] := EnCodeTable[((Packet.a[] shl ) or (Packet.a[] shr )) and $0000003f];
if NumChars < then
OutBuf[] := '='
else
OutBuf[] := EnCodeTable[Packet.a[] and $0000003f];
end;
function DecodePacket(InBuf: PChar; var nChars: Integer): TPacket;
begin
Result.a[] := (DecodeTable[InBuf[]] shl ) or (DecodeTable[InBuf[]] shr );
NChars := ;
if InBuf[] <> '=' then
begin
Inc(NChars);
Result.a[] := Byte((DecodeTable[InBuf[]] shl ) or (DecodeTable[InBuf[]] shr ));
end;
if InBuf[] <> '=' then
begin
Inc(NChars);
Result.a[] := Byte((DecodeTable[InBuf[]] shl ) or DecodeTable[InBuf[]]);
end;
end; procedure EncodeStream(Input, Output: TStream);
type PInteger = ^Integer;
var InBuf: array[..] of Byte;
OutBuf: array[..] of Char;
BufPtr: PChar; I, J, K, BytesRead: Integer;
Packet: TPacket;
begin
K := ;
repeat BytesRead := Input.Read(InBuf, SizeOf(InBuf)); I := ;
BufPtr := OutBuf;
while I < BytesRead do
begin
if BytesRead - I < then
J := BytesRead - I
else
      J := ;
Packet.i := ;
Packet.b0 := InBuf[I];
if J > then
Packet.b1 := InBuf[I + ];
if J > then
Packet.b2 := InBuf[I + ];
EncodePacket(Packet, J, BufPtr);
Inc(I, );
Inc(BufPtr, );
Inc(K, );
if K > then
begin
BufPtr[] := #$0D;
BufPtr[] := #$0A;
Inc(BufPtr, );
K := ;
end;
end;
Output.Write(Outbuf, BufPtr - PChar(@OutBuf));
until BytesRead = ; end; procedure DecodeStream(Input, Output: TStream);
var
InBuf: array[..] of Char;
OutBuf: array[..] of Byte;
InBufPtr, OutBufPtr: PChar;
I, J, K, BytesRead: Integer;
Packet: TPacket; procedure SkipWhite;
var
C: Char;
NumRead: Integer;
begin
while True do
begin NumRead := Input.Read(C, );
if NumRead = then
begin
if C in [''..'','A'..'Z','a'..'z','+','/','='] then
begin
Input.Position := Input.Position - ;
Break;
end;
end
else
Break;
end;
end; function ReadInput: Integer;
var WhiteFound, EndReached : Boolean;
CntRead, Idx, IdxEnd: Integer;
begin
IdxEnd:= ; repeat WhiteFound := False; CntRead := Input.Read(InBuf[IdxEnd], (SizeOf(InBuf)-IdxEnd));
EndReached := CntRead < (SizeOf(InBuf)-IdxEnd);
Idx := IdxEnd; IdxEnd := CntRead + IdxEnd;
while (Idx < IdxEnd) do begin if not (InBuf[Idx] in [''..'','A'..'Z','a'..'z','+','/','=']) then
begin
Dec(IdxEnd);
if Idx < IdxEnd then
Move(InBuf[Idx+], InBuf[Idx], IdxEnd-Idx);
WhiteFound := True;
end
else
Inc(Idx);
end;
until (not WhiteFound) or (EndReached);
Result := IdxEnd;
end;
begin
repeat SkipWhite;
{ BytesRead := Input.Read(InBuf, SizeOf(InBuf)); }
BytesRead := ReadInput;
InBufPtr := InBuf;
OutBufPtr := @OutBuf;
I := ;
while I < BytesRead do
begin
Packet := DecodePacket(InBufPtr, J);
K := ;
while J > do
begin
OutBufPtr^ := Char(Packet.a[K]); Inc(OutBufPtr);
Dec(J);
Inc(K);
end;
Inc(InBufPtr, );
Inc(I, );
end;
Output.Write(OutBuf, OutBufPtr - PChar(@OutBuf)); until BytesRead = ;
end; function EncodeString(const Input: string): string;
var InStr, OutStr: TStringStream;
begin
InStr := TStringStream.Create(Input);
try
OutStr := TStringStream.Create('');
try
EncodeStream(InStr, OutStr);
Result := OutStr.DataString;
finally
OutStr.Free;
end;
finally
InStr.Free;
end;
end; function DecodeString(const Input: string): string;
var InStr, OutStr: TStringStream;
begin
InStr := TStringStream.Create(Input);
try
OutStr := TStringStream.Create('');
try
DecodeStream(InStr, OutStr);
Result := OutStr.DataString;
finally
OutStr.Free;
end;
finally
InStr.Free;
end;
end;
end.

Delphi 自带了 Base64 编解码的单元的更多相关文章

  1. Delphi 自带的 Base64 编解码函数

    今天帮别人解决一个关于 Base64 编解码的问题,竟然发现 Delphi 自带了 Base64 编解码的单元,叫 EncdDecd,这名字很拗口而且不直观,估计这是一直很少人关注和知道的原因. 这个 ...

  2. Delphi Base64 编解码函数

    Delphi 自带 Base64 编解码的单元, EncdDecd这个单元提供两套四个公开函数: 对流的编解码:procedure EncodeStream(Input, Output: TStrea ...

  3. Notepad++插件Base64编解码

    我们平常进行Base64编码需要自己写代码转换, 或者使用其他人编写的小工具程序, 也可以使用在线base64编码工具, 现在我们还可以使用Notepad++自带的插件, 进行Base64编码和解码, ...

  4. ios Base64编解码工具类及使用

    为了避免明码传递http内容,可以用base64编码后传输,收到方再解码,也方便了2进制数据的字符串式传输. 对于ios来说,google给提供了一个很好的工具类,方便进行base64编解码,当然也可 ...

  5. Java实现BASE64编解码

    Java实现BASE64编解码 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs BASE64和其它类似的编码算法通经常使用于转换二进制数据为文本数据,其目 ...

  6. openssl命令行Base64编解码

    openssl对base64编解码的规范支持较差,用它编解码的结果别的语言如php处理很不方便,注意的几点整理如下 1,如果php加密结果做base64编码长度小于64,则需要添加一个换行符opens ...

  7. python rsa 加密解密 (编解码,base64编解码)

    最近有需求,需要研究一下RSA加密解密安全:在网上百度了一下例子文章,很少有文章介绍怎么保存.传输.打印加密后的文本信息,都是千篇一律的.直接在一个脚本,加密后的文本信息赋于变量,然后立马调用解密.仔 ...

  8. python base64 编解码,转换成Opencv,PIL.Image图片格式

    二进制打开图片文件,base64编解码,转成Opencv格式: # coding: utf-8 import base64 import numpy as np import cv2 img_file ...

  9. EasyDarwin开源流媒体云平台中boost Base64编解码后与源长度不匹配的bug

    本文转自EasyDarwin团队Alex的博客:http://blog.csdn.net/cai6811376 EasyDarwin云平台中部分协议使用了Base64编码昨晚报文通信的载体.比如在对摄 ...

随机推荐

  1. Js定义一个表单并提交

    Js定义一个表单 var form = $("<form>"); //定义一个form表单 form.attr('style', 'display:none'); // ...

  2. oracle中not in 和 in的代替用法

    -- not in 的替代写法select col from table1 where col not in(select col from table2); select col,table2.co ...

  3. 泛微e-cology OA Beanshell组件远程代码执行漏洞复现CNNVD-201909-1041

    靶机 影响版本 泛微e-cology<=9.0 https://github.com/jas502n/e-cology 部署 复现 /weaver/bsh.servlet.BshServlet ...

  4. 面向JVM的应用程序的项目结构

    对于Maven所用的项目结构,称为Maven标准的目录结构,不包含git 一.一个典型的源代码结构: / [project-name] README.txt LICENSE.txt pom.xml / ...

  5. 1481:Maximum sum (前缀和+dp)

    [题目描述] 对一个序列A={a1, a2,..., an}给出函数: t1 t2 d(A) = max{ ∑ai + ∑aj | 1 <= s1 <= t1 < s2 <= ...

  6. hdu 6301 Distinct Values (思维+set)

    hdu 6301 Distinct Values 题目传送门 题意: 给你m个区间,让你求出一个长度为n的区间且满足在这些区间的数不重复, 并且要求字典序最小 思路: 如果我们已经求出这个序列了,你会 ...

  7. python学习第三十天函数的形参,实参及函数文档

    python函数的形参是定义函数def 函数名 小括号里面的变量,实参是调用函数时候的值,函数文档是提供函数功能的开发文档,下面 详细说明系列方法 1,函数的形参 def chan(name): pr ...

  8. Codeforces 1012B Chemical table (思维+二分图)

    <题目链接> 题目大意:给定一个n*m的矩阵网格,向其中加点,对于一个组成矩形的四个点中如果有三个点中有元素,那么第四个点中会自动产生新的元素.问你最少再加多少个点能够填满这个网格.解题分 ...

  9. 箭头函数中的this

    箭头函数中的this 箭头函数根据外层(函数或者全局)作用域来决定this 这样this就像其他面向对象的语言,在哪里定义就指向哪里 function foo() { return (x) => ...

  10. JS中 [] == ![]结果为true,而 {} == !{}却为false

     为什么? 先转换再比较      (==) 仅比较而不转换  (===) ==转换规则?   ==比较运算符会先转换操作数(强制转换),然后再进行比较 ①如果有一个操作数是布尔值,则在比较相等性之前 ...