1

我正在使用 MySql 和 Nodejs。我需要存储一个仅由 0 和 1 组成的巨大字符串。(这是一个地图网格。0 = 可以移动,1 = 不能移动。)

Ex: 00000000000000000000000000000000000010000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000010000000000 ...

我应该把那个字符串编码成base64字符串并以这种方式存储吗?然后当我需要它时,我解码它?如果是这样,我将如何使用 Nodejs/Javascript?

谢谢

4

2 回答 2

1

您存储的数据似乎很容易压缩,为什么不使用内置的 MySql COMPRESS功能

生的:

mysql> select length('00000000000000000000000000000000000010000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000010000000000');                                                                                                                                                                                             
332

使用压缩

mysql> select length(compress('00000000000000000000000000000000000010000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000010000000000'));                                                                                                                                                                                                                                                      
23
于 2013-06-19T19:22:53.550 回答
0

不要存储字符串,存储二进制文件。

这是一个可以将该数据转换为二进制的方法示例。希望您可以重新设计事物,以便您的地图数据从一开始就永远不会表示为字符串。

        static void Main(string[] args)
    {
        const string data =
            "00000000000000000000000000000000000010000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000010000000000";

        var arrayLength = data.Length/8;
        if (data.Length%8 != 0)
            arrayLength++;
        var binaryData = new byte[arrayLength];

        byte nextByte=0;
        var k = -1;
        for (var i = 0; i < data.Length; i++)
        {
            var j = i%8;
            if (j == 0)
            {
                binaryData[++k] = nextByte;
                nextByte = 0;
            }

            var bit = int.Parse(data[i].ToString());
            if(bit==1)
                nextByte |= (byte)(bit << j);
        }
        binaryData[k] = nextByte;
    }
于 2013-06-19T18:13:59.597 回答