I need to 'embed' a file as a string of bytes directly in AS3 code rather than calling it as an external resource.
So this works fine:
var testString = "537563636573733a20537472696e672072652d656e636f6465642e";
var testArray:ByteArray = new ByteArray();
var len:uint = testString.length;
trace("testString LENGTH: " + len.toString());
for (var i:uint = 0; i < len; i += 2) {
var c:String = '0x' + testString.charAt(i) + testString.charAt(i + 1);
if(i < 10) { trace("testString Byte: " + c); }
testArray.writeByte(parseInt(c));
}
trace("testString: " + testArray.toString());
trace("testString NUMBER OF BYTES: " + testArray.length.toString());
And produces this in the console:
testString LENGTH: 54
testString Byte: 0x53
testString Byte: 0x75
testString Byte: 0x63
testString Byte: 0x63
testString Byte: 0x65
testString: Success: String re-encoded.
testString NUMBER OF BYTES: 27
So next I open my target file in a Hex editor (HxD) and copy and paste the bytes directly into my String variable, just like above and I get the following output to the console:
testString LENGTH: 97478
testString Byte: 0x50
testString Byte: 0x4B
testString Byte: 0x03
testString Byte: 0x04
testString Byte: 0x14
testString: PK```
testString NUMBER OF BYTES: 48739
...and the file (as a ByteArray) is unreadable by the same library that read it perfectly when it used it as an external resource (using URLLoader.)
I did try copying the byte string back out of my code, pasting it into the Hex editor and saving it as a file and the file was re-created correctly so I don't think it is a copy and paste issue. Also, removing the '0x' from the front of each byte string and parsing with "parseInt(c, 16)" produces exactly the same results.
For some additional background, the target file is a KMZ 3D model and the file is being parsed by Papervision3D's KMZ.as library which uses the Nochump library to unzip the KMZ file. The error message I get back when trying to pass the ByteArray to KMZ.as is:
Error: invalid zip
at nochump.util.zip::ZipFile/findEND()
at nochump.util.zip::ZipFile/readEND()
at nochump.util.zip::ZipFile/readEntries()
at nochump.util.zip::ZipFile()
at org.papervision3d.objects.parsers::KMZ/parse()
at org.papervision3d.objects.parsers::KMZ/load()
at infoModel/initKMZ2()
at infoModel()
Any thoughts and/or suggestions would be greatly appreciated.
fodder