Removing trailing nulls in ActionScript

When reading a byte array in ActionScript (like in C) the null value is read as a string termination. When you try use the readUTFBytes method over a ByteArray if the data contain a null value (0x00) the ActionScript interpreter stop the reading and retunr you a truncated string, in order to avoid this issue you can simply parse the data before using the method readUTFBytes.
Following a small sample where the variable bytedata represents the raw data recovered from your read operation.


var ba:ByteArray = new ByteArray();

for(var i:int = 0; i < bytedata.length; i++){

if(bytedata[i] != 0x00){

ba.writeByte(bytedata[i])

}else{

ba.writeByte(0x0A)

}

}

ba.position = 0;

var data:String = ba.readUTFBytes( ba.length );

Comments on this post

Post a comment

Embed any code between [code] [/code] tags
captcha image   Reload