-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase64Decode
More file actions
24 lines (22 loc) · 961 Bytes
/
base64Decode
File metadata and controls
24 lines (22 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function base64Decode(decodeStr){
if(!decodeStr) return ;
var base64IndexTable = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
var BitPattern='',trueDecodeStr =decodeStr.substring(0,decodeStr.indexOf("=")),
blockNumber= decodeStr.substring(decodeStr.indexOf("=")).length,result='';
for (var i = 0; i < trueDecodeStr.length; i++) {
//得到每个base64字符的索引
var base64Index =base64IndexTable.indexOf(trueDecodeStr[i]);
var binaryStr = (base64Index).toString(2),newBinaryStr='',newBlockBinaryStr='';
for (var j = 0; j < 6-binaryStr.length; j++) {
newBinaryStr += '0';
}
binaryStr = newBinaryStr+binaryStr;
BitPattern += binaryStr;
}
BitPattern =BitPattern.substring(0,BitPattern.length-2*blockNumber);
for (var i = 0; i < BitPattern.length/8; i++) {
var ASCIIChar = parseInt(BitPattern.substring(8*i,8*(i+1)),2);
result += String.fromCharCode(ASCIIChar)
}
return result;
}