ActionScript3中的Int64與ByteArray轉換


ActionScript3中沒有int64(long)類型,因此在與其它語言寫的應用程序進行數據通信及交互時,存在數據轉換的問題。如網絡傳輸中Int64的網絡字符序列要能夠成功接收和發送,而ByteArray中又沒有相應的讀寫方法,只提供了對short,int,uint,double,float,String,Object等數據的類型的read和write方法。內容可參見AS3幫助文檔中的ByteArray類

如果要實現Int64與ByteArray的相互轉換,可實現的方法有很多:

Int64與ByteArray的相互轉換 方法一

//Int64轉換成ByteArray
public function writeInt64(bigInt:Number):ByteArray {
    const zeros:String = "00000000";
    var bytes:ByteArray = new ByteArray();
    var str = bigInt.toString(16);
    str = zeros.substr(0,16-str.length)+str;
    bytes.writeUnsignedInt(parseInt(str.substr(0,8),16));
    bytes.writeUnsignedInt(parseInt(str.substr(8,8),16));
    bytes.position = 0;
    return bytes;
}

//ByteArray轉換成Int64
public function readInt64(bytes:ByteArray):Number {
    const zeros:String = "00000000";
    var s:String = bytes.readUnsignedInt().toString(16);
    var str:String = zeros.substr(0,8-s.length) + s;
    s = bytes.readUnsignedInt().toString(16);
    str += zeros.substr(0,8-s.length) + s ;
    return Number(parseInt(str, 16).toString());
}

 

Int64與ByteArray的相互轉換 方法二

//http://stackoverflow.com/questions/9504487/number-type-and-bitwise-operations
//Int64 to ByteArray
public function writeInt64(n:Number):ByteArray                   
{                                                                         
    // Write your IEEE 754 64-bit double-precision number to a byte array.
    var b:ByteArray = new ByteArray();                                    
    b.writeDouble(n);   
    
    trace("--"+n);
    for(var i:int = 0 ;i<b.length;i++)
    {
        trace(b[i].toString(2));
    }
    trace("--"+n);
                                                      
    var e:int = ((b[0] & 0x7F) << 4) | (b[1] >> 4); // 取得指數 
    var s:int = e - 1023;  // Significant bits. 
    var x:int = (52 - s) % 8; // Number of bits to shift towards the right. 
    var r:int = 8 - int((52 - s) / 8);  // Read and write positions in the byte array.                                    
    var w:int = 8;
    
    // Clear the first two bytes of the sign bit and the exponent.        
    b[0] &= 0x80;                                                         
    b[1] &= 0xF;                                                           
    // Add the "hidden" fraction bit.                                     
    b[1] |= 0x10;                                                      
    // Shift everything.                                                  
    while (w > 1) {                                                       
        if (--r > 0) {                                                    
            if (w < 8)                                                    
                b[w] |= b[r] << (8 - x);                                  
                                                                          
            b[--w] = b[r] >> x;                                           
                                                                          
        } else {                                                          
            b[--w] = 0;                                                   
        }                                                                 
    }                                                                                  
    // Now you've got your 64-bit signed two's complement integer.        
    return b;                                                             
}

//ByteArray to Int64
public function readInt64(bytes:ByteArray):Number {
    //待實現
    return 0;
}

 

對於ActionScript加入對Int64數據的支持,Google Code中有開源的類。

開源類一

開源類二


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM