简体   繁体   English

无法使用javascript解码平铺的base64数据

[英]Trouble decoding tiled base64 data with javascript

I'm using tiled to create a tile map. 我正在使用平铺来创建平铺地图。 (Tiled is a tile map editor with support for orthogonal and isometric maps) (Tiled是一个瓦片地图编辑器,支持正交和等距地图)

It saves the map in an XML file. 它将地图保存在XML文件中。 It can use certain encoding structures: 它可以使用某些编码结构:

  • regular base64 常规base64
  • base64 + gzip base64 + gzip
  • base64 + zlib base64 + zlib
  • regular csv 常规csv

Now, I've completely given up on gzip (my server gzips traffic it anyway, so no loss there) So I thought I'd try regular base64 decoding, using a base64 jquery plugin 现在,我已经完全放弃了gzip(无论如何我的服务器gzips流量,所以没有损失)所以我想我会尝试使用base64 jquery插件进行常规base64解码

But the data comes out all garbled, like this: 但数据出现乱码,如下所示:

��������������������������������������������������������

I guess it's binary encoding it, but how do I get around that? 我猜它是二进制编码,但我该如何解决呢?

Example data that needs to be decoded (regular base64): 需要解码的示例数据(常规base64):

jQAAAI4AAACPAAAAkAAAAJEAAACSAAAAkwAAAKEAAACiAAAAowAAAKQAAAClAAAApgAAAKcAAAA=

Example data that needs to be decoded (gzipped base64): 需要解码的示例数据(gzip base64):

H4sIAAAAAAAACw3DhwnAMAwAMP8P2Rdk9s1KoBQR2WK12R1Ol9vj9fn5A/luZ4Y4AAAA

Example data as csv: 示例数据为csv:

141,142,143,144,145,146,147,
161,162,163,164,165,166,167

So how can I turn the regular base64 encoded bit and turn it into the csv? 那么如何将常规base64编码位转为csv呢?

Edit: 编辑:

Using the solution Pointy found I got a semi-correct array. 使用解决方案Pointy发现我得到了一个半正确的数组。 After a few thousand characters the 2 numbers would be wrong again, though. 但是,在几千个字符后,2个数字将再次出错。 And even more frequent after that. 之后甚至更频繁。

I then found someone who also uses tiled and the base64 encoding in his scheme. 然后我找到了一个在他的方案中也使用了tiles和base64编码的人。 After he decoded the array, he also did this to it: 解码完阵列后,他也这样做了:

        var d = base64_decode($(this).find('data').text());
    var e = new Array();
    for (var i = 0; i <= d.length; i += 4) {
    var f = d[i] | d[i + 1] << 8 | d[i + 2] << 16 | d[i + 3] << 24;
    e.push(f)
    }

I have no idea why this is needed, but at least it works. 我不知道为什么需要它,但至少它是有效的。 If anyone could explain, please do! 如果有人能解释,请做!

Try looking at the returned string character by character. 尝试逐个字符地查看返回的字符串。 In other words, get the "charCodeAt" for each character in the array. 换句话说,为数组中的每个字符获取“charCodeAt”。

function codesFromString(str) {
  var rv = [];
  for (var i = 0; i < str.length; ++i)
    rv.push(str.charCodeAt(i));
  }
  return rv;
}

That leaves you with an array of numbers. 这给你留下了一系列数字。 If you want to dump that out as CSV on a page or something, you can use join() 如果要将其作为CSV转储到页面或其他内容上,可以使用join()

var csv = codeFromString(decoded).join(',');

edit — ok here's a base64 numeric decoder: 编辑 - 好的,这是一个base64数字解码器:

  var base64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split("");
  var base64inv = {}; 
  for (var i = 0; i < base64chars.length; i++) { 
    base64inv[base64chars[i]] = i; 
  }
  function decodeNumeric(s) {
    s = s.replace(new RegExp('[^'+base64chars.join("")+'=]', 'g'), "");

    var p = (s.charAt(s.length-1) == '=' ? 
            (s.charAt(s.length-2) == '=' ? 'AA' : 'A') : ""); 
    var r = []; 
    s = s.substr(0, s.length - p.length) + p;

    for (var c = 0; c < s.length; c += 4) {
      var n = (base64inv[s.charAt(c)] << 18) + (base64inv[s.charAt(c+1)] << 12) +
              (base64inv[s.charAt(c+2)] << 6) + base64inv[s.charAt(c+3)];

      r.push((n >>> 16) & 255);
      r.push((n >>> 8) & 255);
      r.push(n & 255);
    }
    return r;
  }

That works on your sample, but it demonstrates that what you've written as the result is not actually correct. 这适用于您的示例,但它表明您作为结果写的内容实际上并不正确。 Instead of "141,142,143,..." it's "141,0,0,0,142,0,0,0,143,0,0,0" etc. That's what those runs of "AAA" in the encoded string are. 而不是“141,142,143,...”它是“141,0,0,0,142,0,0,0,143,0,0,0”等。这就是编码字符串中那些“AAA”的运行。

(code stolen from http://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64 ) (代码来自http://en.wikibooks.org/wiki/Algorithm_Implementation/Miscellaneous/Base64

老话题,我知道,但你最后一个问题的答案在这里: http//sourceforge.net/apps/mediawiki/tiled/index.php?title = Examining_the_map_format

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM