2011-11-15 33 views
2

現在,當我說的二進制,我的意思ghjkl54╞←‼╝454┴而不是10101110從JS中的文件讀取「二進制」字符?

我想加載一個JavaScript tilemap的,但我不希望重寫我的地圖編輯器,用Java編寫的,其中出口地圖爲二進制文件。所以我想知道,如果可能的話,我該怎麼做?

而且,如果它是不可能的,我應該讀tilemap的結構,其中我只有[width][height][tilenum],[[tilenum2], tilenum3],...]

+1

所以,非人類可讀的。究竟是什麼問題呢? –

回答

2

是,使用HTML5文件API,您可以讀取文件的內容。請注意,這在所有瀏覽器中都尚未支持。

通過使用FileReader和使用readAsBinaryString,您可以得到這些字符:http://jsfiddle.net/S4mEv/3/

// bind a <input type="file">'s onchange event to a function 
// (does not require jQuery, just for convenience here) 
$('#file').change(function() { 
    var fr = new FileReader; // create a file reader 

    fr.onloadend = function() { // when reading has finished 
     alert(fr.result); // alert the contents 
    }; 

    fr.readAsBinaryString(this.files[0]); // start reading 
}); 
1

這個問題對我來說並不很清楚,你需要與實際獲取文件轉換成JavaScript二進制字符串或閱讀文件已在JavaScript的二進制字符串格式幫助?如果後者,也許我的答案可以幫助。

我已經制造和使用在javascript一類爲這些操作是這樣工作的:

//binaryString = result from readAsBinaryString 
    var tileReader = new ByteReader(binaryString), doubles = []; 

     while(!tileReader.EOF()) { 
     doubles.push(tileReader.readDouble()); 
     } //Read the whole file as big endian doubles 

類:

function ByteReader(bytedata) { 
    this._data = bytedata || ""; 
    this._offset = 0; 
    } 

    ByteReader.prototype = { 

     constructor: ByteReader, 

     EOF: function(){ 
     return this._offset >= this._data.length; 
     }, 

     tellSize: function(){ 
     return this._data.length; 
     }, 

     seekTo: function(offset){ 
     this._offset = offset; 
     return this; 
     }, 

     rewind: function() { 
     this._offset = 0; 
     return this; 
     }, 

     readBytes: function(bytes) { 
     var s = this._data.substr(this._offset, bytes); 
     this._offset += bytes; 
     return s; 
     }, 

     setByteStream: function(data) { 

      if(typeof data != "string") 
      throw new TypeError(typeof data + " must be string"); 

     this._data = data; 
     this._offset = 0; 
     return this; 
     }, 

     readDouble: function(littleEndian) { 
     var s = this.readBytes(8); 
     var pow = Math.pow, sign, exponent, fraction; 

      if(littleEndian) 
      s = s.split("").reverse().join(""); 

     sign = (s.charCodeAt(0) & 0x80) >> 7; 
     exponent = ((s.charCodeAt(0) & 0x7F) << 4) | ((s.charCodeAt(1) & 0xF0) >> 4); 
     fraction = ((s.charCodeAt(1) & 0x0F) * pow(2, 48)) + 
       s.charCodeAt(2) * pow(2, 40) + 
       s.charCodeAt(3) * pow(2, 32) + 
       ((s.charCodeAt(4) & 0xFF) << 24) + 
       ((s.charCodeAt(5) & 0xFF) << 16) + 
       ((s.charCodeAt(6) & 0xFF) << 8 ) + 
       s.charCodeAt(7); 

     sign = pow(-1, sign); 

      if(exponent === 2047) { 
       if(f !== 0) 
       return Number.NaN; 

       else if(sign < 0) 
       return -Infinity; 

       else 
       return Infinity; 
      } 
      else if(exponent > 0) 
      return sign * Math.pow(2, exponent - 1023) * (fraction/0x10000000000000 + 1); 


      else if (fraction !== 0) 
      return sign * Math.pow(2, -1022) * (fraction/0x10000000000000); 


      else 
      return 0; 

     }, 

     readSingle: function(littleEndian) { 
     var s = this.readBytes(4) 
     var sign, exponent, fraction; 

      if(littleEndian) 
      s = s.split("").reverse().join(""); 

     sign = (s.charCodeAt(0) & 0x80) >> 7; 
     exponent = ((s.charCodeAt(0) & 0x7F) << 1) | ((s.charCodeAt(1) & 0x80) >> 7); 
     fraction = ((s.charCodeAt(1) & 0x7F) << 16) | 
       ((s.charCodeAt(2) & 0xFF) << 8) | 
       (s.charCodeAt(3) & 0xFF); 

     sign = Math.pow(-1, sign); 

      if(exponent === 255) { 

       if(fraction !== 0) 
       return Number.Nan; 

       else if(sign < 0) 
       return -Infinity; 

       else 
       return Infinity; 
      } 
      else if(exponent > 0) 
      return sign * Math.pow(2, exponent - 127) * (fraction/0x800000 + 1); 

      else if (fraction !== 0) 
      return sign * Math.pow(2, -126) * (fraction/0x800000); 

      else 
      return 0; 

     }, 

     readSByte: function() { 
     var s = this.readBytes(1).charCodeAt(0) & 0xFF; 
     return (s^0x80) - 0x80; 
     }, 

     readUByte: function() { 
     return this.readBytes(1).charCodeAt(0) & 0xFF; 
     }, 

     readUShort: function(littleEndian) { 
     var s = this.readBytes(2); 

      if(littleEndian) 
      return (s.charCodeAt(0) & 0xFF) | 
      ((s.charCodeAt(1) & 0xFF) << 8); 

      else 
      return (s.charCodeAt(1) & 0xFF) | 
      ((s.charCodeAt(0) & 0xFF) << 8); 
     }, 

     readULong: function(littleEndian) { 
     var s = this.readBytes(4), r; 

      if(littleEndian) 
      r = (s.charCodeAt(0) & 0xFF)  | 
      ((s.charCodeAt(1) & 0xFF) << 8) | 
      ((s.charCodeAt(2) & 0xFF) << 16) | 
      ((s.charCodeAt(3) & 0xFF) << 24); 

      else 
      r = (s.charCodeAt(3) & 0xFF)  | 
      ((s.charCodeAt(2) & 0xFF) << 8) | 
      ((s.charCodeAt(1) & 0xFF) << 16) | 
      ((s.charCodeAt(0) & 0xFF) << 24); 

      if (r & 0x80000000) 
      r = (r & 0x7FFFFFFF) + 0x80000000; 

     return r; 
     }, 

     readSShort: function(littleEndian){ 
     var s = this.readBytes(2), r; 

      if(littleEndian) 
      r = (s.charCodeAt(0) & 0xFF) | 
      ((s.charCodeAt(1) & 0xFF) << 8); 

      else 
      r = (s.charCodeAt(1) & 0xFF) | 
      ((s.charCodeAt(0) & 0xFF) << 8); 

     return (r^0x8000) - 0x8000; 
     }, 

     readSLong: function(littleEndian){ 
     var s = this.readBytes(4), r; 

      if(littleEndian) 
      return (s.charCodeAt(0) & 0xFF) | 
      ((s.charCodeAt(1) & 0xFF) << 8) | 
      ((s.charCodeAt(2) & 0xFF) << 16) | 
      ((s.charCodeAt(3) & 0xFF) << 24); 

      else 
      return (s.charCodeAt(3) & 0xFF) | 
      ((s.charCodeAt(2) & 0xFF) << 8) | 
      ((s.charCodeAt(1) & 0xFF) << 16) | 
      ((s.charCodeAt(0) & 0xFF) << 24); 

     } 


    }; 

已經與.WAV文件測試了很多。