function encode(source)
{
var i = 0;
var chr2 = 0;
var chr3 = 0;
var chr1 = 0;
var enc2 = 0;
var enc3 = 0;
var enc4 = 0;
var enc1 = 0;
if(!source || source == "")
{
return "";
}
var size = source.length;
var output = "";
while(i < size)
{
i++;
chr1 = source.charCodeAt(i);
i++;
chr2 = source.charCodeAt(i);
i++;
chr3 = source.charCodeAt(i);
enc1 = chr1 >> 2;
enc2 = (chr1 & 3) << 4 | chr2 >> 4;
enc3 = (chr2 & 15) << 2 | chr3 >> 6;
enc4 = chr3 & 63;
if(isNaN(chr2))
{
enc4 = 64;
enc3 = 64;
}
else if(isNaN(chr3))
{
enc4 = 64;
}
output = output + ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(enc1) + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(enc2));
output = output + ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(enc3) + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(enc4));
}
return output;
}
function encodeByteArray(data)
{
var i = 0;
var l = 0;
var dataBuffer = null;
if(!data)
{
return "";
}
var pos = data.position;
var output = "";
var outputBuffer = new Array(4);
data.position = 0;
while(data.bytesAvailable > 0)
{
dataBuffer = [];
i = int(0);
while(i < 3 && data.bytesAvailable > 0)
{
dataBuffer[i] = data.readUnsignedByte();
i++;
}
outputBuffer[0] = (dataBuffer[0] & 252) >> 2;
outputBuffer[1] = (dataBuffer[0] & 3) << 4 | dataBuffer[1] >> 4;
outputBuffer[2] = (dataBuffer[1] & 15) << 2 | dataBuffer[2] >> 6;
outputBuffer[3] = dataBuffer[2] & 63;
l = dataBuffer.length;
for(i = l; i < 3; )
{
outputBuffer[i + 1] = 64;
i++;
}
l = outputBuffer.length;
for(i = int(0); i < l; )
{
output = output + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".charAt(outputBuffer[i]);
i++;
}
}
data.position = pos;
return output;
}
function decode(source)
{
var chr2 = 0;
var chr3 = 0;
var chr1 = 0;
var enc2 = 0;
var enc3 = 0;
var enc4 = 0;
var enc1 = 0;
var i = 0;
if(!source || source == "")
{
return "";
}
var size = source.length;
var output = "";
while(i < size)
{
i++;
enc1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(source.charAt(i));
i++;
enc2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(source.charAt(i));
i++;
enc3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(source.charAt(i));
i++;
enc4 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(source.charAt(i));
chr1 = enc1 << 2 | enc2 >> 4;
chr2 = (enc2 & 15) << 4 | enc3 >> 2;
chr3 = (enc3 & 3) << 6 | enc4;
output = output + String.fromCharCode(chr1);
if(enc3 != 64)
{
output = output + String.fromCharCode(chr2);
}
if(enc4 != 64)
{
output = output + String.fromCharCode(chr3);
}
}
return output;
}
function decodeToByteArray(data)
{
var i = 0;
var j = 0;
var k = 0;
var output = new ByteArray();
var dataBuffer = new Array(4);
var outputBuffer = new Array(3);
var length = data.length;
i;
while(i < length)
{
j = uint(0);
while(j < 4 && i + j < length)
{
dataBuffer[j] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".indexOf(data.charAt(i + j));
j++;
}
outputBuffer[0] = (dataBuffer[0] << 2) + ((dataBuffer[1] & 48) >> 4);
outputBuffer[1] = ((dataBuffer[1] & 15) << 4) + ((dataBuffer[2] & 60) >> 2);
outputBuffer[2] = ((dataBuffer[2] & 3) << 6) + dataBuffer[3];
for(k = uint(0); k < 3; )
{
if(dataBuffer[k + 1] == 64)
{
break;
}
output.writeByte(outputBuffer[k]);
k++;
}
i = uint(i + 4);
}
output.position = 0;
return output;
}
问一下大佬这个js脚本中encodeByteArray为什么输出总是空?
最新推荐文章于 2022-09-23 19:28:39 发布