python实现base64解码_在Python中将base64数据解码为数组

我使用这个方便的Javascript函数来解码base64字符串并得到一个数组作为返回。在

这是字符串:base64_decode_array('6gAAAOsAAADsAAAACAEAAAkBAAAKAQAAJgEAACcBAAAoAQAA')

返回的内容如下:

^{pr2}$

问题是我不太了解javascript函数:var base64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split("");

var base64inv = {};

for (var i = 0; i < base64chars.length; i++) {

base64inv[base64chars[i]] = i;

}

function base64_decode_array (s)

{

// remove/ignore any characters not in the base64 characters list

// or the pad character -- particularly newlines

s = s.replace(new RegExp('[^'+base64chars.join("")+'=]', 'g'), "");

// replace any incoming padding with a zero pad (the 'A' character is zero)

var p = (s.charAt(s.length-1) == '=' ?

(s.charAt(s.length-2) == '=' ? 'AA' : 'A') : "");

var r = [];

s = s.substr(0, s.length - p.length) + p;

// increment over the length of this encrypted string, four characters at a time

for (var c = 0; c < s.length; c += 4) {

// each of these four characters represents a 6-bit index in the base64 characters list

// which, when concatenated, will give the 24-bit number for the original 3 characters

var n = (base64inv[s.charAt(c)] << 18) + (base64inv[s.charAt(c+1)] << 12) +

(base64inv[s.charAt(c+2)] << 6) + base64inv[s.charAt(c+3)];

// split the 24-bit number into the original three 8-bit (ASCII) characters

r.push((n >>> 16) & 255);

r.push((n >>> 8) & 255);

r.push(n & 255);

}

// remove any zero pad that was added to make this a multiple of 24 bits

return r;

}

这些“&lt;&lt;”和“&gt;&gt;”字符的功能是什么。

或者Python有这样的函数吗?在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值