问题
How to convert Little-endian ByteArray to Big-endian in AS3? I convert bitmapData to Big-endian ByteArray and then push it into memory with Adobe Alchemy. And then when i read it from memory i get Little-endian ByteArray. How to get Big-endian.
I use this example code http://blog.debit.nl/2009/03/using-bytearrays-in-actionscript-and-alchemy/ (Memory allocation in C with direct access in Actionscript (FAST!!))
Code:
var ba:ByteArray = currentBitmapData.getPixels( currentBitmapData.rect );
ba.position = 0;
var ns:Namespace = new Namespace("cmodule.al_exam");
var data:ByteArray = (ns::gstate).ds; //point to memory
_dataPosition = lib.initByteArray(ba.length); //This is the position of the data in memory
lib.writeData(ba); //example function to write data in C
data.position = _dataPosition; //reset position
// get noise, not image
bmd.setPixels(currentBitmapData.rect,data);
myBitmap.bitmapData = bmd;
回答1:
A while back I asked about this on the Adobe Forums.
The Flash VM itself is Little Endian, as is code running in Alchemy. For reasons that are unclear, many Flash APIs return bytes as Big Endian. How bizarre. (Perhaps this is a legacy API thing from the Macromedia days. Who knows?)
Setting ByteArray.endian does not change the bytes stored in the ByteArray. It affects the way that readXXX/writeXXX works (that is, they will flip the bytes as they're read/written).
However, since your Alchemy code is looking at the bytes directly (ie, not using the ByteArray methods), it has to handle the flipping manually. Note that this is true for incoming bytes as well outgoing bytes.
Some more info about flipping byte order is here.
来源:https://stackoverflow.com/questions/6315417/convert-little-endian-bytearray-to-big-endian-in-as3