字符串压缩,先zlib再base64转码示例(以Delphi为例)

1. 简介:
这是常用的网络通信的手段,原因是: zlib 压缩可以将字符串体积明显缩小(只有较长才能体现出来),而base64可以将刚刚压缩的二进制码变成可见字符,便于在语言中进行传递及网络通信。

2. 关于zlib压缩算法的使用:
在linux 用C语言开发一般用zlib 库
在Delphi5上 可以用zlibEx version 1.2.3 july 19, 2007 ? 156 kb (因为更新的版本将不再适合Delphi5, 而着重为Delphi7及2010优化)
在其它高级语言上基本都有库函数,可以直接用

3. 关于base64算法
首先预备知识:base64算法介绍:http://baike.baidu.com/view/469071.htm
以Delphi5中为例,定义:
   Base64Table      : String[Base64TableLength] =
      'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
   Pad              = '=';

base64算法虽然只有一种,但是有人会变码表。主要是针对加号“+”和 左斜杠“/”  根据需要替换成了别的字符, 还有的换pad的等于号成其它,要特别注意。
字符串压缩,先zlib再base64转码示例(以Delphi为例) - huasoft - 快乐的机器猫 小桥加加网易分站

3. 转换前后的字符串例子:
源字符串:
{ "req_type": "T", "old_time": "1970-01-01 00:17:04", "org_code": "10101049" }
转码结果:(引号之内)
eJyrVlAqSi2ML6ksSFWyUlAKUdJRUMrPSYkvycwFCxhamhvoGhgCkYKBgZWhuZWBCVhJUXp8cn4KRIkBCJpYKinUAgBJlBQA


4. 附录:在Delphi下的base64转换与逆向转换源码
function TForm1.B64Encode(const s: string): string;
var
  i,c1,c2,c3: Integer;
  m,n: Integer;
const
  Base64: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
begin
  Result := '';
  m:=1;
  n:=0;
  for i := 1 to (Length(s) div 3) do
  begin
    c1 := Ord(s[m]);
    c2 := Ord(s[m+1]);
    c3 := Ord(s[m+2]);
    m:=m+3;
    Result := Result+base64[(c1 shr 2)and $3F+1];
    Result := Result+base64[((c1 shl 4)and $30) or ((c2 shr 4)and $0F)+1];
    Result := Result+base64[((c2 shl 2)and $3C) or ((c3 shr 6)and $03)+1];
    Result := Result+base64[c3 and $3F+1];
    n:=n+4;
    if(n = 76)then
    begin
       n:=0;
       Result := Result+#13#10;
    end;
  end;
  if (Length(s) mod 3)=1 then
  begin
    c1 := Ord(s[m]);
    Result := Result+base64[(c1 shr 2)and $3F+1];
    Result := Result+base64[(c1 shl 4)and $30+1];
    Result := Result+'=';
    Result := Result+'=';
  end;
  if (Length(s) mod 3)=2 then
  begin
    c1 := Ord(s[m]);
    c2 := Ord(s[m+1]);
    Result := Result+ base64[(c1 shr 2)and $3F+1];
    Result := Result+ base64[((c1 shl 4)and $30) or ((c2 shr 4)and $0F)+1];
    Result := Result+base64[(c2 shl 2)and $3C+1];
    Result := Result+ '=';
  end;
end;

function TForm1.B64Decode(const s: string): string;
var
  i,m,n: Integer;
  c1,c2,c3,c4: Integer;
const
  Base64: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
begin
  Result := '';
  n:=1;
  m:=Length(s);
  if s[m]='='then m:=m-1;
  if s[m]='='then m:=m-1;
  for i:=1 to m div 4 do
  begin
    c1:=Pos(s[n],Base64)-1;
    c2:=Pos(s[n+1],Base64)-1;
    c3:=Pos(s[n+2],Base64)-1;
    c4:=Pos(s[n+3],Base64)-1;
    n:=n+4;
    Result:=Result+Chr(((c1 shl 2)and $FC)or((c2 shr 4)and $3));
    Result:=Result+Chr(((c2 shl 4)and $F0)or((c3 shr 2)and $0F));
    Result:=Result+Chr(((c3 shl 6)and $C0)or c4);
  end;
  if m mod 4=2 then
  begin
    c1:=Pos(s[n],Base64)-1;
    c2:=Pos(s[n+1],Base64)-1;
    Result:=Result+Chr(((c1 shl 2)and $FC)or((c2 shr 4)and $3));
  end;

  if m mod 4=3 then
  begin
    c1:=Pos(s[n],Base64)-1;
    c2:=Pos(s[n+1],Base64)-1;
    c3:=Pos(s[n+2],Base64)-1;
    Result:=Result+Chr(((c1 shl 2)and $FC)or((c2 shr 4)and $3));
    Result:=Result+Chr(((c2 shl 4)and $F0)or((c3 shr 2)and $0F));
  end;

end;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值