base64标准编码的解码的java实现 已经给出了Ddelphi的实现

tangxin123 2011-07-11 01:39:16

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;



求java代码 只需要解码
...全文
174 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tangxin123 2011-07-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 tangxiao890923 的回复:]
Java code


public class Base64 {
private static final String base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

public static final String decode(final String……
[/Quote]
嗯 验证了 是对的 谢谢
tangxiao890923 2011-07-12
  • 打赏
  • 举报
回复

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());
}
}
tangxin123 2011-07-12
  • 打赏
  • 举报
回复
楼上的 那些貌似都不是标准编码的
williamxiao 2011-07-11
  • 打赏
  • 举报
回复
下个工具包commons-codec,在org.apache.commons.codec.binary包中有Base64类,
使用其中的public static byte[] decodeBase64(String base64String)函数.
lyhmy 2011-07-11
  • 打赏
  • 举报
回复
不懂,友情帮顶
bianhei000 2011-07-11
  • 打赏
  • 举报
回复
不是有API么 直接调之

51,408

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧