小端的ByteArray转换为大端在AS3大端、转换为、ByteArray

2023-09-08 13:08:51 作者:一点情一点缘

如何小端的ByteArray转换为大端在AS3? 我转换成位图数据以大端的ByteArray然后将其推入内存与Adobe炼金术。然后,当我从内存中读取它,我得到小端的ByteArray。如何获得大端。

我用这个例子code HTTP ://blog.debit.nl/2009/03/using-bytearrays-in-actionscript-and-alchem​​y/ (内存分配在C中与ActionScript(FAST!)直接访问)

code:

 变种BA:的ByteArray = currentBitmapData.getPixels(currentBitmapData.rect);
ba.position = 0;

VAR NS:命名空间=新的命名空间(cmodule.al_exam);
VAR数据:ByteArray中=(NS :: gstate)的.ds; //指向内存

_dataPosition = lib.initByteArray(ba.length); //这是在存储器中的数据的位置
lib.writeData(BA); //例如函数写在C数据
data.position = _dataPosition; //复位位置

//获取噪音,没有图像
bmd.setPixels(currentBitmapData.rect,数据);
myBitmap.bitmapData = BMD;
 

解决方案

前阵子我问这对的的Adobe论坛的。

闪存虚拟机本身是Little Endian,因为是code炼金术运行。其原因尚不清楚,许多闪存API返回字节大端。如何离奇。 (也许这是从Macromedia天。谁知道一个遗留API的事情吗?)

设置ByteArray.endian做的没有的修改存储在ByteArray中字节。它会影响的readXXX / writeXXX工作的方式(即,它们将翻转字节,因为它们可以读/写)。

然而,由于你的炼金术code是看着字节直接(即不使用ByteArray方法),它必须手动处理的翻转。请注意,这是真正的进入的字节数,也传出字节。

有关翻转字节顺序有些信息是这里。

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;

解决方案

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.