1
<script>
// This would be the place to edit if you want a different
// Base32 implementation
var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
/**
* Build a lookup table and memoize it
*
* Return an object that maps a character to its
* byte value.
*/
var lookup = function() {
var table = {}
// Invert 'alphabet'
for (var i = 0; i < alphabet.length; i++) {
table[alphabet[i]] = i
}
lookup = function() { return table }
return table
}
// Functions analogously to Encoder
function Decoder() {
var skip = 0 // how many bits we have from the previous character
var byte = 0 // current byte we're producing
this.output = ''
// Consume a character from the stream, store
// the output in this.output. As before, better
// to use update().
this.readChar = function(char) {
if (typeof char != 'string'){
if (typeof char == 'number') {
char = String.fromCharCode(char)
}
}
//char = char.toLowerCase()
var val = lookup()[char]
if (typeof val == 'undefined') {
// character does not exist in our lookup table
return // skip silently. An alternative would be:
// throw Error('Could not find character "' + char + '" in lookup table.')
}
val <<= 3 // move to the high bits
byte |= val >>> skip
skip += 5
if (skip >= 8) {
// we have enough to preduce output
this.output += String.fromCharCode(byte)
skip -= 8
if (skip > 0) byte = (val << (5 - skip)) & 255
else byte = 0
}
}
this.finish = function(check) {
var output = this.output + (skip < 0 ? alphabet[bits >> 3] : '') + (check ? '$' : '')
this.output = ''
return output
}
}
Decoder.prototype.update = function(input, flush) {
for (var i = 0; i < input.length; i++) {
this.readChar(input[i])
}
var output = this.output
this.output = ''
if (flush) {
output += this.finish()
}
return output
}
/** Convenience functions
*
* These are the ones to use if you just have a string and
* want to convert it without dealing with streams and whatnot.
*/
// Base32-encoded string goes in, decoded data comes out.
function decode(input) {
var decoder = new Decoder()
var output = decoder.update(input.split("").reverse().join("")+'A', true)
return output
}
function toHex(str) {
var hex = '';
for(var i=0;i<str.length;i++) {
//hex += ''+("00" + str.charCodeAt(i).toString(16)).substr(-2);
hex += str.charCodeAt(i).toString(16);
}
return hex;
}
convertHex = toHex(decode('A0C4KB'));
alert(convertHex);
</script>
上述脚本在FF和Chrome上正常工作,并给出正确的十六进制值。 警报输出将其视为与Javascript未能在IE中输出正确的输出
预计abc2d0
对于IE,这似乎并没有工作。我得到的所有
FFFFFFFF
这是一个Base32实现,我从https://github.com/agnoster/base32-js
在IE9和IE8中正常工作 - 我假设你使用的是IE7?解决方案=升级浏览器这里是每个人的小提琴:http://jsfiddle.net/neuroflux/DSvGj/ –
@Neurofluxation:OP可能能够更新他/她的浏览器,但不是客户端的浏览器。我完全理解必须处理旧引擎的挫折感,但所有实现都支持'.charAt',并且您不必强迫用户离开暗边,即IE –
这可能是机器与最新的补丁,你使用的是神经...我在一台老机器上测试了IE8和IIRC IE9无法正常工作。 –