DELPHI自带的字符串编码解码单元EncdDecd

时间:2010-01-29 08:56:17  来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:rcolordark="#ffffff" cellpadding="5" width="99%" bgcolor="#ffffc0" border="1">{********************************************************}{Borland Delphi Visual Component Library     }{ Copyright (c) 2000, 2001 Borland Software Corporation  }{********************...
  正文:大家恐怕还不知道Delphi自带了一个字符串编码解码的单元吧,这个文件就在
C:Program FilesBorlandDelphi7SourceInternet
下,Delphi2005也在同样的路径下.
下面我们就来看看这段代码:

{********************************************************}
{Borland Delphi Visual Component Library     }
{ Copyright (c) 2000, 2001 Borland Software Corporation  }
{********************************************************}
unit EncdDecd;

{ Have string use stream encoding since that logic wraphotoshop/ target=_blank class=infotextkey>ps 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[0..63] of Char =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
'abcdefghijklmnopqrstuvwxyz' +
'0123456789+/';

DecodeTable: array[#0..#127] of Integer = (
Byte('='), 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64);

type
PPacket = ^TPacket;
TPacket = packed record
case Integer of
0: (b0, b1, b2, b3: Byte);
1: (i: Integer);
2: (a: array[0..3] of Byte);
3: (c: array[0..3] of Char);
end;

procedure EncodePacket(const Packet: TPacket; NumChars: Integer; OutBuf: PChar);
begin
  OutBuf[0] := EnCodeTable[Packet.a[0] shr 2];
  OutBuf[1] := EnCodeTable[((Packet.a[0] shl 4) or (Packet.a[1] shr 4)) and $0000003f];
  if NumChars < 2 then
  OutBuf[2] := '='
  else OutBuf[2] := EnCodeTable[((Packet.a[1] shl 2) or (Packet.a[2] shr 6)) and $0000003f];
  if NumChars < 3 then
  OutBuf[3] := '='
  else OutBuf[3] := EnCodeTable[Packet.a[2] and $0000003f];
end;

function DecodePacket(InBuf: PChar; var nChars: Integer): TPacket;
begin
  Result.a[0] := (DecodeTable[InBuf[0]] shl 2) or
  (DecodeTable[InBuf[1]] shr 4);
  NChars := 1;
  if InBuf[2] <> '=' then
  begin
    Inc(NChars);
    Result.a[1] := Byte((DecodeTable[InBuf[1]] shl 4) or (DecodeTable[InBuf[2]] shr 2));
  end;
  if InBuf[3] <> '=' then
  begin
    Inc(NChars);
    Result.a[2] := Byte((DecodeTable[InBuf[2]] shl 6) or DecodeTable[InBuf[3]]);
  end;
end;

procedure EncodeStream(Input, Output: TStream);
type
PInteger = ^Integer;
var
InBuf: array[0..509] of Byte;
OutBuf: array[0..1023] of Char;
BufPtr: PChar;
I, J, K, BytesRead: Integer;
Packet: TPacket;
begin
  K := 0;
  repeat
  BytesRead := Input.Read(InBuf, SizeOf(InBuf));
  I := 0;
  BufPtr := OutBuf;
  while I < BytesRead do
  begin
    if BytesRead - I < 3 then
    J := BytesRead - I
    else J := 3;
    Packet.i := 0;
    Packet.b0 := InBuf[I];
    if J > 1 then
    Packet.b1 := InBuf[I + 1];
    if J > 2 then
    Packet.b2 := InBuf[I + 2];
    EncodePacket(Packet, J, BufPtr);
    Inc(I, 3);
    Inc(BufPtr, 4);
    Inc(K, 4);
    if K > 75 then
    begin
      BufPtr[0] := #$0D;
      BufPtr[1] := #$0A;
      Inc(BufPtr, 2);
      K := 0;
    end;
  end;
  Output.Write(Outbuf, BufPtr - PChar(@OutBuf));
  until BytesRead = 0;
end;

"DELPHI自带的字符串编码解码单元EncdDecd"由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Language/Delphi/12582.html


关键字:

关于《DELPHI自带的字符串编码解码单元EncdDecd》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td