Convert ByteArray to Integer in Flex
Can someo开发者_如何转开发ne shed some lights on how to convert ByteArray into int?
Thanks,
So to get a flavor of doing this, you can try this bit of code:
function test(){
var bytes:ByteArray = new ByteArray();
bytes.writeInt(0x00DDAA99); //create my byte array with int 14527129
bytes.position = 0; //move the postion to the start
var newInt:int = bytes.readInt(); //read the bytes from starting position
trace("new num: "+newInt); //print out the number
}
This code will first create a byte array and put an int into it. This is presumably where you need your code to start. This then makes the assumption that there are 4 bytes to read after the starting position which I have set to 0. It then reads the 4 bytes off the byte array into the queue. Note that if you do not have 4 bytes in your ByteArray or your position is not set correctly, your code will fail. Make sure you add the checks for those scenarios.
This code also assumes that the byte array is Big Endian. Make sure that if you have a byte array from another system, that you know which endian-ness the int value has. Change the endian value on your byte array if needed.
精彩评论