Search code examples
actionscript-3encodingbase64tiledtmx

How to decode Base64(uncompressed) TMX data element contents?


I have been trying to decode the Base64 contents of the data element in a TMX file with AS3. I am using mx.utils.Base64Decoder to decode the contents.

Here is my 1x1 layer data without the encoding (I'm not sure if the encoded data is in XML format):

<tile gid="1"/>

Here is my layer encoded:

AQAAAA==

When I try to decode the data, the return value is empty. I tried to decode the data with an online Base64 decoder but that showed an empty value also.


Solution

  • The base64-encoded data is binary, where each 8 bytes are a 32-bit unsigned little-endian global tile ID, in your case 1. The decoded data can hence not be directly represented as a string.

    The byte values of the data in your case would be:

    1 0 0 0
    

    Since the ASCII value for 1 is a control character meaning SOH (Start of Heading) and 0 is generally used to mark the end of the string, nothing will display when you try to print this. You should instead combine these bytes to the 32-bit unsigned global tile id, as follows:

    unsigned global_tile_id = data[tile_index] |
                              data[tile_index + 1] << 8 |
                              data[tile_index + 2] << 16 |
                              data[tile_index + 3] << 24;
    

    See the TMX format reference for more details and the full example code: