我正在使用这个方便的Javascript函数来解码base64字符串并获得一个数组作为回报。
这是字符串:
base64_decode_array('6gAAAOsAAADsAAAACAEAAAkBAAAKAQAAJgEAACcBAAAoAQAA')
这是返回的内容:
234,0,0,0,235,0,0,0,236,0,0,0,8,1,0,0,9,1,0,0,10,1,0,0,38,1,0,0,39,1,0,0,40,1,0,0
问题是我真的不懂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;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;和“&gt;&gt;&gt;”字符。 或者Python有这样的函数吗?
答案 0 :(得分:10)
谁在乎呢。 Python有更简单的方法来做同样的事情。
[ord(c) for c in '6gAAAOsAAADsAAAACAEAAAkBAAAKAQAAJgEAACcBAAAoAQAA'.decode('base64')]
答案 1 :(得分:5)
在Python中我希望你只使用base64 module ...
...但是回答您关于<<
和>>>
的问题:
<<
是左移算子;结果是第一个操作数向左移动了第二个操作数的位数;例如5 << 2
是20
,因为5是二进制的101,而20是10100。>>>
是非符号扩展的右移运营商;结果是第一个操作数被移位
右边第二个操作数的位数......最左边的位总是用0填充。答案 2 :(得分:3)
为什么不呢:
from binascii import a2b_base64, b2a_base64
encoded_data = b2a_base64(some_string)
decoded_string = a2b_base64(encoded_data)
def base64_decode_array(string):
return [ord(c) for c in a2b_base64(string)]
答案 3 :(得分:2)
只是为了好玩/完整,我会更简单地翻译javascript ::)
# No particular reason to make a list of chars here instead of a string.
base64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
lookup = dict((c, i) for (i, c) in enumerate(base64chars))
def base64_decode_array(s):
# Filter out meaningless chars, especially newlines. No need for a regex.
s = ''.join(c for c in s if c in base64chars + '=')
# replace any incoming padding with a zero pad (the 'A' character is zero)
# Their way:
# p = ('AA' if s[-2] == '=' else 'A') if s[-1] == '=' else ''
# s = s[:len(s) - len(p)] + p
# My way (allows for more padding than that;
# '=' will only appear at the end anyway
s = s.replace('=', 'A')
r = []
# Iterate over the string in blocks of 4 chars - an ugly hack
# though we are preserving the original code's assumption that the text length
# is a multiple of 4 (that's what the '=' padding is for) ;)
for a, b, c, d in zip(*([iter(s)] * 4)):
# Translate each letter in the quad into a 6-bit value and bit-shift them
# together into a 24-bit value
n = (lookup[a] << 18) + (lookup[b] << 12) + (lookup[c] << 6) + lookup[d]
# split the 24-bit number into the original three 8-bit (ASCII) characters
r += [(n >> 16) & 0xFF), (n >> 8) & 0xFF), (n & 0xFF)]
return r