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中有开源的类。

开源类一

开源类二

你可能感兴趣的:(actionscript)