0

I am writing alot of single bytes into a byte Array. Is there any way to compact this and write them all at once?

myByteArray.writeByte(0x00);
myByteArray.writeByte(0x00);
myByteArray.writeByte(0x00);
myByteArray.writeByte(0x01);
myByteArray.writeByte(0x00);
myByteArray.writeByte(0x10);

4 Answers 4

2

Could create a Vector and list your values there, then just write them in from there.

var values:Vector.<uint> = new <uint>[0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x10];

for each(var i:uint in values)
{
    myByteArray.writeByte(i);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Another option, if you are doing this a lot, is to create a file that has the sequence of bytes that you want, embed that file into your swf as a byte array, and then use .writeBytes(). Something like:

[Embed(source="data.dat", mimeType="application/octet-stream")]
private const Fragment:Class;

And then:

var wad:ByteArray = new Fragment() as ByteArray;
myByteArray.writeBytes(wad);

Comments

1

ByteArray does not support [] notation. The only thing, that comes to my mind is the following:

var myByteArray:ByteArray=new ByteArray();
var tempArr:Array=[0x00, 0x00, 0x00, 0x01, 0x00, 0x10];
for each (var elem:int in tempArr)
    myByteArray.writeByte(elem);

Comments

0

Feel free to vote this down, as this is utterly ridiculous :).

myByteArray.writeUnsignedInt(0x00 << 24 | 0x00 << 16 | 0x00 << 8 | 0x01);

You would save 3 function calls :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.