How to create the base64 data on tmx files? ( xCode )

Hey!

Im just new with tiled.

Im creating a Tmx file using xcode directly.

All working great if I use pure xml on data tag :

    NSMutableString *xml = [[NSMutableString alloc]init];
// header
    [xml appendFormat:@"<map version=\"1.0\" orientation=\"orthogonal\" width=\"30\" height=\"30\" tilewidth=\"64\" tileheight=\"64\" >\n"];
// tilesets..
    [xml appendFormat:@"<tileset firstgid=\"1\" name=\"Floor\" tilewidth=\"64\" tileheight=\"64\">\n"];
    [xml appendFormat:@"<image source=\"Floor@2x.png\" width=\"64\" height=\"64\"/>\n"];
    [xml appendFormat:@"</tileset>"];
// layers
    [xml appendFormat:@"<layer name=\"Flo\" width=\"30\" height=\"30\">\n"];
    [xml appendFormat:@"<data>\n"];
    NSMutableString *Data;
    NSString *GidTile;
    for(int i=0;i<899;i++)
    {
        GidTile=[NSString stringWithFormat:@"1"];
        [Data appendFormat:@"<tile gid=\"%@\">",GidTile];
    }
    [xml appendFormat:theString];
    [xml appendFormat:@"</data>"];
    [xml appendFormat:@"</layer>"];
    [xml appendFormat:@"</map>"];

if I open the file created with Tiled all works as expected. It generates a tmx with the correct format. The problems comes when I want to use base64 to encode the data. Im using the following function to encode the “Data” NSstring on base64:

- (NSString *)base64String:(NSString *)str
{
    NSData *theData = [str dataUsingEncoding: NSASCIIStringEncoding];
    const uint8_t* input = (const uint8_t*)[theData bytes];
    NSInteger length = [theData length];
    
    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    
    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;
    
    NSInteger i;
    for (i=0; i < length; i += 3) {
        NSInteger value = 0;
        NSInteger j;
        for (j = i; j < (i + 3); j++) {
            value <<= 8;
            
            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }
        
        NSInteger theIndex = (i / 3) * 4;
        output[theIndex + 0] =                    table[(value >> 18) & 0x3F];
        output[theIndex + 1] =                    table[(value >> 12) & 0x3F];
        output[theIndex + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[theIndex + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }
    
    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}

But its don’t give me the same base64 string that Tile does If I save the exact map using the application. I don’t really get it :frowning: I would be very happy if someone can help with this!

Regards & thanks for your time! :smiley:

Seems noone knows nothing about :frowning: , But I really don’t understand what exactly base64 algorithm is using Tile or how it is encoding it…

For example if you make a tmx map ( map size : 2x2 - tile size : 64x64) with TILED and you paint it all with the same tile, you will get a -data- like this one:

On base64 : AQAAAAEAAAABAAAAAQAAAA==

Ok great! If you now decode that string , you will get something like this:

<01000000 01000000 01000000 01000000>

Now… lets encode it back using the base64 python encoder:

s = '01000000 01000000 01000000 01000000’
b = base64.b64encode(s)

The result of b would to be the same as before… but no… now b is:

MDEwMDAwMDAgMDEwMDAwMDAgMDEwMDAwMDAgMDEwMDAwMDA=

Sooooo… What exactly you need to do to obtain the exact same string that TILED obtain from the base64 encoding?

Thanks!

Hey @Lukas182 Have you sorted out this problem? I’m trying to understand how to decode the data zipped sting but without any success. I’m using c# but the algorithm will be pretty the same.

Nop! No way :frowning: . My problem here is to encode the data string on base64 ( using xcode - cocos2d ) to add it on data tag inside the tmx file but I have already used a bunch of functions to encode it and theres no way to obtain the same result that TILED app do. Then Im unable to open the tmx generated :frowning: .

Note that tiled also can do compression. I am not sure what you are trying to do exactly (base64 only, base64+zlib, base64+gzip?). But make sure to either compare to the right map format that tiled saves (you can edit it under Map Properties -> Tile Layer Format or also compress according to the desired compression type.

Regards,
Ablu