base64 string 放不下_base64转换string

1.通过函数转

function Base64ToStr1(const Base64: string): string;

var

I, J, K, Len, Len1: Integer;

B4: array[0..3] of Byte;

begin

if Base64 = '' then

begin

Result := '';

Exit;

end;

Len := Length(Base64);

if Len and 3 <> 0 then

raise Exception.Create('Invalid Base64 length');

Len1 := (Len shr 2) * 3;

SetString(Result, nil, Len1);

I := 1;

J := 1;

while I <= Len do

begin

for K := 0 to 3 do

begin

case Base64[I] of

'A'..'Z':

B4[K] := Ord(Base64[I]) - 65;

'a'..'z':

B4[K] := Ord(Base64[I]) - 71;

'0'..'9':

B4[K] := Ord(Base64[I]) + 4;

'+':

B4[K] := 62;

'/':

B4[K] := 63;

'=':

B4[K] := 0;

else

raise Exception.CreateFmt('#%d: Invalid char in Base64', [Ord(Base64[I])]);

end;

Inc(I);

end;

Result[J] := Chr((B4[0] shl 2) or (B4[1] shr 4));

Result[J + 1] := Chr((B4[1] shl 4) or (B4[2] shr 2));

Result[J + 2] := Chr((B4[2] shl 6) or B4[3]);

Inc(J, 3);

end;

I := Pos('=', Base64);

if I <> 0 then

begin

I := Len - I + 1;

Delete(Result, Len1 - I + 1, I);

end;

end;

2.用字符串流转

sst := TStringStream.Create(str);

sst.Position :=0;

ms := TMemoryStream.Create;

DecodeStream(sst,ms);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值