Neon Flash
Neon Flash

Reputation: 3233

ActionScript 3.0 - Null Bytes in ByteArray

I am trying to understand the significance of null bytes in a ByteArray. Do they act like a terminator? I mean, can we not write further into the ByteArray once a null byte has been written?

For instance,

import flash.utils.*;

public class print3r{
    public function print3r{
        Util.print(nullout());
    }

    public function nullout:ByteArray (){
        var bytes:ByteArray = new ByteArray();
        bytes.writeInt(((403705888 + 1) - 1)); // Non Printable Characters
        bytes.writeInt(((403705872 - 1) + 1)); // Non Printable Characters
        bytes.writeInt(0x18101000); // Notice the NullByte in this DWORD
        bytes.writeInt(0x41424344); // ASCII Characters ABCD
        return bytes;
    }
}

new print3r;

This gives a blank output.

Now, if I replace the DWORD, 0x18101000 with 0x18101010, this time I can see the ASCII padding, ABCD in the output.

My question is that, is it possible to write past the null byte into the ByteArray()?

The reason I ask is because I have seen in an ActionScript code, that a lot of writeInt and writeByte operations are performed on the ByteArray even after the null byte is written.

Thanks.

Upvotes: 2

Views: 1999

Answers (2)

Michael Antipin
Michael Antipin

Reputation: 3532

is it possible to write past the null byte into the ByteArray()?

Of course it is. ByteArray -- is a chunk of raw data. You can write whatever you like there, and you can read in whatever way you like (using zero bytes as delimiters or whatever else you may want to do).

What you see when you send your bytes to standard output with trace(), depends solely on what you actually do with your data to convert it to a string. There are several ways of converting an array of bytes to string. So, your question is missing the explanation of what Util.print() method does.

Here are several options for converting bytes to a string:

  • Loop through bytes and output characters, encoding is up to you.
  • Read a string with ByteArray.readUTFBytes(). This method reads utf-encoded symbols; it stops when zero character is encountered.
  • Read a string with ByteArray.readUTF(). This method expects your string to be prefixed with unsigned short indicating its length. In other terms it is the same as ByteArray.readUTFBytes().
  • Use ByteArray.toString(). This is what happens when you simply do trace(byteArray);. This method ignores zero bytes and outputs the rest. This method uses System.useCodePage setting to decide on the encoding, and can use UTF BOM if the data begins with it.

Here are some tests that illustrate the above:

var test:ByteArray = new ByteArray();

// latin (1 byte per character)
test.writeUTFBytes("ABC");

// zero byte
test.writeByte(0);

// cyrillic (2 bytes per character)
test.writeUTFBytes("\u0410\u0411\u0412");

trace(test); // ABCАБВ
trace(test.toString()); // ABCАБВ
test.position = 0;
trace(test.readUTFBytes(test.length)); // ABC

// simple loop
var output:String = "";
var byte:uint;
for (var i:uint = 0; i<test.length; i+=1) {
    byte = uint(test[i]);
    if (output.length && i%4 == 0) {
        output += " ";
    }
    output += (byte > 0xF ? "" : "0") + byte.toString(16);
}
trace(output); // 41424300 d090d091 d092

Upvotes: 5

Jonatan Hedborg
Jonatan Hedborg

Reputation: 4432

Writing a null to a byte array has no significance as far as I know. The print function might however use it as a string terminator.

Upvotes: 1

Related Questions