2012-09-17 2 views
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에서 잘 작동하며 정확한 16 진수 값을 제공합니다.Javascript가 IE에서 정확한 결과를 제공하지 못함

abc2d0

예상대로 경고 출력은 IE를 들어,이 작동하지 않습니다 온다. 나는 모든

FFFFFFFF

이 내가 문자열 상수에 배열 액세스를 지원하지 않습니다 https://github.com/agnoster/base32-js

+1

IE9와 IE8에서 잘 작동합니다. IE7을 사용하고 있다고 가정합니까? 해결책 = 브라우저를 업그레이드하십시오. 모든 사람들을위한 바이올린은 다음과 같습니다. http://jsfiddle.net/neuroflux/DSvGj/ –

+0

@Neurofluxation : OP가 클라이언트의 브라우저는 아니지만 브라우저를 업데이트 할 수 있습니다. 오래된 엔진을 다루어야한다는 좌절감을 절대적으로 이해하고 있지만, .charAt는 모든 구현에서 지원됩니다. 사용자가 어두운면을 떠나도록 강제하지 않아도됩니다. –

+0

그건 아마 당신이 Neuro를 사용하고있는 최신 패치가있는 컴퓨터 ... 나는 IE8과 IIRC IE9가 설치된 구형 컴퓨터에서 이것을 시험해 보았습니다. –

답변

2

인터넷 익스플로러 (Internet Explorer)의 JScript 엔진에서 최대 pickep Base32의 구현을 얻는다. 작동 시키려면 alphabet[i]alphabet.charAt(i)으로 대체해야합니다. 나는 MS가이 문제를 다루었다고 생각했지만, 나는 틀릴 수도/너무 희망적 일 수있다.

관련 문제