(Edited the title because I had no idea what I was looking for and it was misleading.)
Edit: What I was looking for was binary to string and back again. I have answered my own question below.)
Original Post: I'm trying to make a retro-style password system for a game made with JavaScript. (like on an old NES game for example that uses alpha-numeric characters to load the level you were on or all the flags pertaining to that level.)
I've gotten so far as generating a string of flags (all numeric) and then loading that string later by sorting through those flags with regex and then putting them back into my gamestate object (with objects in it that hold all my various flags).
Each flag is a number between 0-9 and each object (or group of flags) are 8 characters long. (often with leading zeros, so these groups are always 8 characters long)
A typical string could look like this:
var gameStr = "000102340000001000019531";
(shown in groups to illustrate them individually)
00010234
00000010
00019531
(3 groups of 8 characters for example)(24 characters long) (but will probably end up having upwards of 25-ish groups of 8 when the game is finished)
As you can imagine, this number is going to get pretty long and obviously wouldn't work as a user inputted password.
So I started looking online for ways to compress this number.
I'm hoping to compress it to something a user could easily copy and paste into a tweet or a chat message, something that doesn't look too "ugly" and isn't too long (I don't know, I'm not being picky here, it could be anywhere between 6-24 characters?) and I don't mind if it's easily unencrypted- security is not important for this use case. If necessary, I would be willing to change the rules, for example the way the numbers are stored like groups of 4 flags/digits each. I'm merely looking for a way to make this number smaller either mathematically or through some kind of compression algorithm.
I came across two seemingly promising solutions,
The first was this JavaScript library called lz-string It's like LZW but faster and more specific, it compresses a string into a hex code that looks something like:
Input:
000102340000001000019531000102340000001000019531000102340000001000019531
(72 characters)
(9 groups of 8 characters separated just to visualise the numbers in their groups)
00010234
00000010
00019531
00010234
00000010
00019531
00010234
00000010
00019531
Output:
0803c08c199858808e5f807a059c936258190d2c2d438ec6b43c4c5d0080
(spaces removed)(60 characters)
But as you can see, the hex is still pretty long.
So the second solution I found was this quiet answer tucked away on SO:
Jamie Morgan:
What about converting a big number to a formula: So instead of 21312312312 I might use 4^34
(they provided a link to some math forum, but the link is dead.)
And this, in my head, seems like it could work, but I don't have the knowledge to know how to even start writing such a function that could do that.. (math REALLY isn't my strong suit..) this idea seems like the math equivalent of "unfrying an egg"..
So my question is, Any ideas on how I can shorten this number or compress it into a compressed number (or string) and then back again?
As an aside, I would like to mention that I've spent almost a week googling and looking at other answers in SO to this kind of question and so far I'm beginning to think it might just be impossible.. if you have reason to believe that it is impossible, please just tell me so I can stop looking for an answer.. I could easily just save this data to the browser's localStorage
and be done with it, but I thought a password system would be more authentic and a fun challenge to incorporate and to learn a little about working with compression and numbers in this way.
Thank you in advance for your understanding and any help you may be able to provide.