Decrypt array to string
arrays, javascript, jquery, url
Solution
You can use the `atob` and `btoa` functions.
myArray = ["screen", "left_side", "left_side", "right_side", "left_side", "right_side", "left_side", "right_side"]
btoa(JSON.stringify(myArray)) // "WyJzY3JlZW4iLCJsZWZ0X3NpZGUiLCJsZWZ0X3NpZGUiLCJyaWdodF9zaWRlIiwibGVmdF9zaWRlIiwicmlnaHRfc2lkZSIsImxlZnRfc2lkZSIsInJpZ2h0X3NpZGUiXQ=="
You can then convert it back into the orignal array
array = JSON.parse(atob(str))
If you include LZString (as mentioned in the comments), you can get shorter strings.
var str = btoa(JSON.stringify(myArray)); // 132 characters
var str = LZString.compressToBase64(JSON.stringify(myArray)); // 72 characters
var str = LZString.compressToBase64(myArray.toString()); // 64 characters
To uncompress,
array = JSON.parse(LZString.decompressFromBase64(str));
fiddle
Problem
I currently have an array that looks like this: ``` ["screen", "left_side", "left_side", "right_side", "left_side", "right_side", "left_side", "right_side"] ``` I now want to encrypt this somehow so I can use it as a URL, like: `http://www.site.com/app.html?array=...` this is because I want to allow users to share their arrays. Is there any way to encrypt an array to it's usable in a URL string and decrypt it later on?