51,409
社区成员
发帖
与我相关
我的任务
分享
function Base64Decode(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;
public class Base64 {
private static final String base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
public static final String decode(final String s){
int i,m,n,c1,c2,c3,c4;
n = 0;
m = s.length();
ByteBuffer bu = ByteBuffer.allocate(m-(m/4));
if(s.charAt(m-1)=='='){
m--;
}
if(s.charAt(m-1)=='='){
m--;
}
for(i=1;i<=m/4;i++){
c1 = base64.indexOf(s.charAt(n));
c2 = base64.indexOf(s.charAt(n+1));
c3 = base64.indexOf(s.charAt(n+2));
c4 = base64.indexOf(s.charAt(n+3));
n+=4;
bu.put((byte)(((c1<<2)&0xFC)|((c2>>4)&0x3)));
bu.put((byte)(((c2<<4)&0xF0)|((c3>>2)&0x0F)));
bu.put((byte)(((c3<<6)&0xC0)|c4));
}
if(m%4==2){
c1 = base64.indexOf(s.charAt(n));
c2 = base64.indexOf(s.charAt(n+1));
bu.put((byte)(((c1<<2)&0xFC)|((c2>>4)&0x3)));
}
if(m%4==3){
c1 = base64.indexOf(s.charAt(n));
c2 = base64.indexOf(s.charAt(n+1));
c3 = base64.indexOf(s.charAt(n+2));
bu.put((byte)(((c1<<2)&0xFC)|((c2>>4)&0x3)));
bu.put(((byte)(((c2<<4)&0xF0)|((c3>>2)&0x0F))));
}
return EncodingUtil.bytes2HexString(bu.array());
}
}