MV Converting a 30+ Digit Number into a Short Alphanumerical Passcode

Hello!

I've got a bit of a tricky problem I'm trying to solve. In one of my projects, I'm implementing a system to allow players to generate and share codes that will let the creatures they raise inhabit the other player's game. To do this though, I need to pack a lot of info regarding the creature into a code.

At the moment, the absolute shortest I've been able to crunch the passcode down to is 36 digits, all consisting of numbers 0-9. This is a bit of a long code to expect the player to have to deal with, but I was wondering if there was any way this massive string of numbers could be run through a small script, that could convert it into a much shorter code made using both letters and numbers? Perhaps even less yet, if capital letters can be implemented as well!

I'd appreciate any help I could get with making this system less of a chore for the players to interact with.

Thank you!
at that level, there are limits on how far you can reduce data. A direct shift would have it only go down to about 25 alphanumeric digits instead of 36 numerical digits.
it is usually much more effective to get the data shift before the current 0-9 data structure and compress there.
how did you create the current 36-digit-number from the data?
You just got to cipher the text using unique characters to replace double and triple-digit characters.
I.E.: 11 = !; 12 = @, 13 = $
As a more practical example, using a table containing 231 unique characters in Excel to reduce a 36 digit number: 177652168228334921459893234629821429
Would give you an output of 16 characters: ÈWï8û7GíQo3–D<í3
That's 50% of the size.
In theory, by using an even bigger table you could further reduce this number, the bigger the table of substitution, the longer it takes to solve, and the smaller the number becomes.
Maybe someone well versed in java can give you a hand, or you could just adapt your output to replace decimals for unique characters.
2022-04-28 06_59_27-Table.xlsx - Excel (Error de activación de productos).png
Rather than use the digits 0-9 you could use the digits 0-F, then each digit could hold 16 possible values instead of 10 (0-9, A-F), but if the data is already made, and is already a 36 digit number, there isn't a whole lot of shortening you could do to it after the fact without changing either how the data is encoded, or how it is decoded.

As an example, if your 'creature' has 36 different attributes, and each one can have up to 10 values, the smallest, easiest way to encode that is the way you already have.

You could probably use some kind of script to encode a javascript object into some kind of stringified code, though, which might turn that same creature into a giant string you could use to import it into a different project, but that code is likely going to be hundreds of characters long.

You could take the result number and convert it to hex, or some other number system, one with base 62 (10 numbers + 26 upper and lower case characters) could hold a lot more data in a smaller 'passcode', that might even end up being human readable. Of course, you'd also need to be able to decode that passcode on the other end, and I just randomly googled a decimal to base64 converter, and it seemed like no matter what 36 digit number I put into it, it always came back as AA==, so uhh, good luck with that.
Note that JavaScript's uses the IEEE 754 format for its Number type, which is only precise to 16~17 decimal digits (52-bit mantissa). So unless you're generating the "number" as a string of digits or using a type like BigInt, you might experience some loss of information before considering a change of representation. More technical details here:
Base-64 is what the RMMV core scripts use for compressing stringified save data (via the LZString library). If you have your base-10 passcode in a string, you can try something like this to convert to/from base-64 respectively:
JavaScript:
LZString.compressToBase64(data)
JavaScript:
LZString.decompressFromBase64(data)
E.g.

LZString.compressToBase64("123456789012345678901234567890") // returns "IwJgzALArAbA7ADgJwAZSVo17r2SoA=="
Edit: just realised that's wrong (LZString is a compression library, not a base-conversion tool), so I crossed it out...you'll need a method that treats the input as 10 states per character rather than however many the string encoding uses. It seems native BigInt support is unavailable by default in RMMV v1.6.2, since it uses Node v9.11.1 (process.versions.node in console to check). So you'll probably need something that operates on strings. 😴
I think you want the following scripts:

JavaScript:
// Number to password
let number = 123456789;
let password = number.toString(36); // password: 21i3v9

// Password to number
number = parseInt(password, 36); // number: 123456789

The first converts a number to a string with radix 36 (where "z" represents the number 35).
The second reverses that process using the radix 36 (where 35 represents the letter "z").

This does not differentiate between uppercase and lowercase letters, so "at" = "aT" = "At" = "AT" = 389

This works with any radix between 2 and 36, by the way. For instance, a radix of 2 would be used for binary, and a radix of 16 for hexadecimal.

To treat lowercase and uppercase differently, we'd need a radix of 62. Since the JavaScript built-in functions don't support that, we'll need to implement our own.

Here's an example for converting numbers to a password:
JavaScript:
function numToPassword(num) {
    let result = "";
    while (num) {
        const remainder = num % 62;
        let char = "";
        if (remainder <= 9) {
            char = String(remainder);
        } else if (remainder >= 10 && remainder <= 35) {
            char = String.fromCharCode(remainder - 10 + "A".charCodeAt(0));
        } else {
            char = String.fromCharCode(remainder - 36 + "a".charCodeAt(0));
        }
        result = char + result;
        num = (num - remainder) / 62;
    }
    return result || "0";
}

let number = 123456789;
let password = numToPassword(number); // password: 8M0kX

And here's one to convert the password back:
JavaScript:
function passwordToNum(password) {
    let result = 0;
    for (let c of password) {
        let num = 0;
        if (/[0-9]/.test(c)) {
            num = Number(c);
        } else if (/[A-Z]/.test(c)) {
            num = c.charCodeAt(0) - "A".charCodeAt(0) + 10;
        } else if (/[a-z]/.test(c)) {
            num = c.charCodeAt(0) - "a".charCodeAt(0) + 36;
        }
        result = result * 62 + num;
    }
    return result;
}

let password = "8M0kX";
let number = passwordToNum(password); // number: 123456789
@Nolonar even if that works, just for clarification.
you put the top part int a plugin (except the last 2 bottom lines)?
which you use inside the game to make them?

because out of curiously, if this does work, it could help
to generate a dynamic password, but I need an example how
to use it in an event. (I can DM you for an example I need) if I may?
@Nolonar even if that works, just for clarification.
you put the top part int a plugin (except the last 2 bottom lines)?
which you use inside the game to make them?

You can either copy the entire code block into a script call, or put both functions in a plugin to use later.

Here's an example with Script call:
Screenshot 2022-04-28 211623.png


In the above example, I'm reading the number from variable #0001 and saving the password to variable #0002.


Here's an example with plugin:
Copy paste the following code block into the plugin:
JavaScript:
function numToPassword(num) {
    let result = "";
    while (num) {
        const remainder = num % 62;
        let char = "";
        if (remainder <= 9) {
            char = String(remainder);
        } else if (remainder >= 10 && remainder <= 35) {
            char = String.fromCharCode(remainder - 10 + "A".charCodeAt(0));
        } else {
            char = String.fromCharCode(remainder - 36 + "a".charCodeAt(0));
        }
        result = char + result;
        num = (num - remainder) / 62;
    }
    return result || "0";
}
function passwordToNum(password) {
    let result = 0;
    for (let c of password) {
        let num = 0;
        if (/[0-9]/.test(c)) {
            num = Number(c);
        } else if (/[A-Z]/.test(c)) {
            num = c.charCodeAt(0) - "A".charCodeAt(0) + 10;
        } else if (/[a-z]/.test(c)) {
            num = c.charCodeAt(0) - "a".charCodeAt(0) + 36;
        }
        result = result * 62 + num;
    }
    return result;
}

And then use the functions within a Control Variables command:
Screenshot 2022-04-28 212211.png



because out of curiously, if this does work, it could help
to generate a dynamic password, but I need an example how
to use it in an event. (I can DM you for an example I need) if I may?

Feel free.


Note:
Fair warning, I only tested the code in MZ. If it doesn't work for MV, don't hesitate to tell me and I'll see to it that it works in MV as well.
@Nolonar's suggestion is very elegant. I would probably just use that, but I was thinking about the problem and liked what @NexoNeko proposed because it's something that I could actually code lol. Anyway, since it's 36 characters, you can get that down to 18 chars if you're willing to lose 1 point of precision in your scale, so you'd have 0-8 (or, to disguise it a bit, 1-9) instead of 0-9.

00 = 0
01 = 1
...
10 = a
...
61 = Z
...
89 = "

You can get 89 chars just on the keyboard (US anyway), no need for oddball characters while also avoiding ambiguous things like ` and '

345687340924345687340924345687340924 = yU:y9oyU:y9oyU:y9o

One playful idea would be to craft in-game characters with text emojis built into their trait strings.

As for encoding into Base64, I think that will just make your string longer. I ran 36 digit number through and got a 48-char result.

Lastly, starting from square one (this probably isn't practical, was just thinking about it), game designers sometimes fool players into thinking there is more flexibility in a system than there really is. You could couple the variables/traits in such a way that if one goes up, the corresponding one must go down or is in some other way dependent. If trait A = 3 then trait B = A + 1. Now you can cut the initial 36 down to 18 with these coupled variables. Then, that 18 can be reduced to 9 chars via the above method. That 9 is the lowest I can see possibly going and it's a bit of a cheat.
Thank you very much everyone for your suggestions and support! 🙏

how did you create the current 36-digit-number from the data?
Currently, I track all of the creature's values by making them each an actor, and using the plugin Bob's Custom Stats. When generating the passcode, I set a handful of temporary variables to the creature's values, add trailing zeros depending on which digit of the code that bit of info would be, and add them all together. I've been able to get away with combining a few of the smaller values that only can be a couple of values, which has helped a bit.

I think you want the following scripts:
Thank you very much! I've tried out both scripts you've provided, and they both work great, so long as I break up the code a bit. Like @caethyril mentioned, it seems that I don't end up with a whole number if I plug the entire 36 digit code into the script, however if I break the code up into lengths of 16 or less, it works just fine. This actually shouldn't be an issue, though. I could either break up the current 36 digit code into 3 "lines" of 12 digits, or if I can manage to shrink the info down into 32 digits, 2 lines of 16 digits.
I've been able to get away with combining a few of the smaller values that only can be a couple of values, which has helped a bit.
This is what you would have to do for every bit of the numbers to really reduce the length of the code.
But that would require you to not only know about bit operations, but be comfortable enough with them to pull off splitting bits of one number to go into different letters of the final code.

and you should specify one point that might have been missed by some people:
How do you transfer the code?

From my read on the description you want the player to manually enter the code - and that would make some of the solutions above less functional because most player have no idea how to get special ASCII letters of other country languages into their computer.
Solutions like the one proposed by @NexoNeko will only work if the string is transferred by text file, not if the player has to put it in by keyboard.
and you should specify one point that might have been missed by some people:
How do you transfer the code?
Ah, that's a good point.

The way I've got it set up, the player is indeed inputting the passcode themselves. By using Name Input Processing, I have them type out the code (each "line" is a separate actor's name). Then, I save those actor's names into variables, add trailing zeros as needed, and add the variables together to get the 36 digit code.

Original thread: https://forums.rpgmakerweb.com/threads/converting-a-30-digit-number-into-a-short-alphanumerical-passcode.147145/