javascript - Decrypt array to string -


i have array looks this:

["screen", "left_side", "left_side", "right_side", "left_side", "right_side", "left_side", "right_side"] 

i want encrypt somehow can use url, like: http://www.site.com/app.html?array=... because want allow users share arrays.

is there way encrypt array it's usable in url string , decrypt later on?

you can use atob , 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 convert orignal array

array = json.parse(atob(str)) 

if include lzstring (as mentioned in comments), can 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


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -