Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Efficient way to store text with only 0s and 1s

1. ## Efficient way to store text with only 0s and 1s

I need to store a huge string made of only 0s and 1s.
Ex: 0000000000000000000000000000000000001000000000000000000000001000000000000000000000000000000000000000 0000000000000000000000000000000010000000000000000000000010000000000000000000000000000000000000000000 0000000000000000000000000000100000000000000000000000100000000000000000000000000000000000000000000000 00000000000000000000000010000000000
...

Is there another alternative than using BLOB/MEDIUMBLOB to do so? I don`t need any regular letter or numbers.

Thanks

EDIT: Storing as a string doesn`t work because it converts my 0s and 1s into their equivalent in base 2...

2. Originally Posted by IdkWhatsRc
Storing as a string doesn`t work because it converts my 0s and 1s into their equivalent in base 2...
WHAT????

Storing as a string converts them into *CHARACTERS* "1" and "0" and that has nothing whatsoever to do with *ANY* numeric base.

Storing them as BITs (e.g., in a BINARY field) does indeed convert to base 2. But so what? Why does that bother you?

Not to ask a dumbassed question: What in the world is this *FOR*? I'm betting there's a better solution, all around.

3. Originally Posted by Old Pedant
WHAT????

Storing as a string converts them into *CHARACTERS* "1" and "0" and that has nothing whatsoever to do with *ANY* numeric base.

Storing them as BITs (e.g., in a BINARY field) does indeed convert to base 2. But so what? Why does that bother you?

Not to ask a dumbassed question: What in the world is this *FOR*? I'm betting there's a better solution, all around.
The list of 0s and 1s represents a map grid. 0 = can move, 1 = can't walk.
I want to be able to do something like:
if(grid[player.position] == 1)
{ collision() }

Problem with BINARY is that the maximum size in MySQL is too small to store all my grid.

4. What about VARBINARY? That can be up to 64KB in size.

But I don't see what is wrong with VARCHAR or even TEXT.

So what if the data is stored as a string?

Code:
`if(grid[player.position] == 1)`
you just use
Code:
`if( grid.charAt(player.position) == "1")`
Or, for that matter, for all browsers except some ancient ones, you *can* treat a string as an array of characters, so you would just use
Code:
`if(grid[player.position] == "1")`
Or, as a minor hack, you could do:
Code:
```var grid = ...string you get from the database ...
grid = grid.split(""); // converts the string into an array!```

5. ## Users who have thanked Old Pedant for this post:

IdkWhatsRc (06-20-2013)

6. Okay. Solution found. I'm using COMPRESS() and UNCOMPRESS() built-in functions of MySql.

Code:
```client.query('select uncompress(grid) from map where id=3',function(err, results) {
console.log(   results[0]['uncompress(grid)'].toString()[player.position]  ); //0 or 1
});```
I'm converting my 9216 of length string of 0s and 1s into a 629 of length compressed blob.

7. Why do you think that is "efficient"???

It takes *TIME* to compress and uncompress.

Unless your DB table is huge, who cares about saving a lousy 8600 bytes or so per record????

Say you had 10,000 people playing the game at the same time, you'd be saving 86,000,000 bytes of disk space. Under 100 megabytes. On a disk that probably has hundreds of *GIGABYTES* of storage. Whoopee.

Most of the time, trading disk space for cpu time is a *good* tradeoff.

Besides, you will *STILL* be sending all 9216 bytes of data back and forth from JavaScript to the server. And *THAT* will be a bigger performance hit than anything. If you wanted real efficiency, you'd do the compress and uncompress in the JavaScript code.

Which would be pretty trivial to do.

Heck, you could do an 8-to-1 compression just by using the bits in a string of bytes instead of using entire bytes.

8. Originally Posted by Old Pedant
Why do you think that is "efficient"???

It takes *TIME* to compress and uncompress.

Unless your DB table is huge, who cares about saving a lousy 8600 bytes or so per record????

Say you had 10,000 people playing the game at the same time, you'd be saving 86,000,000 bytes of disk space. Under 100 megabytes. On a disk that probably has hundreds of *GIGABYTES* of storage. Whoopee.

Most of the time, trading disk space for cpu time is a *good* tradeoff.

Besides, you will *STILL* be sending all 9216 bytes of data back and forth from JavaScript to the server. And *THAT* will be a bigger performance hit than anything. If you wanted real efficiency, you'd do the compress and uncompress in the JavaScript code.

Which would be pretty trivial to do.

Heck, you could do an 8-to-1 compression just by using the bits in a string of bytes instead of using entire bytes.
Actually, I'm only 'loading' the maps once for all the players when the server is initialized. All the players are using the same maps collision grid.

9. My head hurts. So then why does the map need to be in a database table?

Or will you have several possible maps?

And why would you only use an on/off indicator? Why wouldn't you do something like
Code:
```var map = [
["ffffwwwwCCCC"],
["ffwwwgggCppC"],
...
];```
where each letter is used to indicate what is there on the map? (e.g., f==forest, w==water, C==castle, g==grass, p==path).

And make it a 2D array, instead of just 1D, so the you can easily reference it via `map[playerYposition][playerXposition]`??

10. ## Users who have thanked Old Pedant for this post:

IdkWhatsRc (06-20-2013)

11. Originally Posted by Old Pedant
My head hurts. So then why does the map need to be in a database table?

Or will you have several possible maps?

And why would you only use an on/off indicator? Why wouldn't you do something like
Code:
```var map = [
["ffffwwwwCCCC"],
["ffwwwgggCppC"],
...
];```
where each letter is used to indicate what is there on the map? (e.g., f==forest, w==water, C==castle, g==grass, p==path).

And make it a 2D array, instead of just 1D, so the you can easily reference it via `map[playerYposition][playerXposition]`??
That's what I was doing before when my game was only solo. When I think about it, I guess you're right, I probably don't need to put my grid in my DB if I'm going to copy it only once when the game starts. I don't really know why I really wanted it in a DB in the first place...

Thanks.

EDIT: If you're wondering, here is my game (solo) with its source code. http://www.youtube.com/watch?v=8EaAiazT2K4

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•