J
J
J. Snow2020-05-13 13:38:11
MySQL
J. Snow, 2020-05-13 13:38:11

How to convert BASE62 to BINARY?

Hi all!

In my application, I generate Base62 unique identifiers with a length of 20 characters and store them in MySQL.

Identifier examples:

xQkmfxYVBLicFvaw2ECL
5JhHXRYSSI6lVnOxzgOX
... and so on

MySQL spends 1 byte per character. But this is base62, which means we use only 62 values ​​in each byte out of 256 possible.

Question:
How to convert base62 to bytes and store them as BINARY ?

The goal is to use the entire range of values ​​in each byte (0-255).

PS: Java code examples are welcome.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Boev, 2020-05-13
@j-snow

Don't get a big win. 62 and 256 are 6 and 8 bits respectively, i.e. savings up to 25%.
How to convert, there are several options:
1) convert each character in the string to a binary code of 6 bits, pad the resulting string to a length multiple of 8, convert 8 bits into characters.
2) Make a table of 4 base62 characters, which will uniquely match 3 base256 characters. Complement the string to a length multiple of 4 characters, take 4 characters each and from the table they will correspond to 3 base256 characters
.
The general rule that I came up with for myself at school: if a, b are number systems and ifa^N = b^M , then every N characters in system a is replaced by M characters in system b .
For example:
convert 10101010101010101010101 from binary to 8ary system: [2^3 = 8^1]
(0)10 101 010 101 010 101 010 101 (_2) = 25252525 (_8)
convert 102301023001230 from 4th to 8ary system: [2^3 = 8^2]
102 301 023 001 230 (_4) = 22 61 13 01 54 (_8)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question