我正在尝试将ArrayBuffer javascript对象编码为base32 String,但没有成功。 有没有人对我如何做到这一点有任何建议?这是我现在拥有的。 我已经修改了base32-js项目中的一些代码。
var alphabet = '0123456789abcdefghjkmnpqrtuvwxyz'
var alias = { o:0, i:1, l:1, s:5 }
var lookup = function() {
var table = {}
for (var i = 0; i < alphabet.length; i++) {
table[alphabet[i]] = i
}
for (var key in alias) {
if (!alias.hasOwnProperty(key)) continue
table[key] = table['' + alias[key]]
}
lookup = function() { return table }
return table
}
function Encoder() {
var skip = 0
var bits = 0
this.output = ''
this.readByte = function(byte) {
if (typeof byte == 'string') byte = byte.charCodeAt(0)
if (skip < 0) {
bits |= (byte >> (-skip))
} else {
bits = (byte << skip) & 248
}
if (skip > 3) {
skip -= 8
return 1
}
if (skip < 4) {
this.output += alphabet[bits >> 3]
skip += 5
}
return 0
}
this.finish = function(check) {
var output = this.output + (skip < 0 ? alphabet[bits >> 3] : '') + (check ? '$' : '')
this.output = ''
return output
}
}
Encoder.prototype.update = function(input, flush) {
for (var i = 0; i < input.length; ) {
i += this.readByte(input[i])
}
// consume all output
var output = this.output
this.output = ''
if (flush) {
output += this.finish()
}
return output
}
function encode(input) {
var encoder = new Encoder()
var output = encoder.update(input, true)
return output
}
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://www.richerlife.com/wp-content/uploads/2012/01/google-logo-682-571408a.jpg', false);
xhr.overrideMimeType("text/plain; charset=x-user-defined");
xhr.onload = function(e) {
if (this.status == 200) {
alert(encode(xhr.responseText));
}
};
xhr.send();