When we have a large number of data to be numbered, and the number of digits limit, such as 5-bit license plate number, 10-bit ID number, order flow number, short URL, etc., we can use 36 to calculate the number of matched digits.
We will 0-z (0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ) to represent the value 0-35, as the letter Z represents 35. So I'm going to get a 5-bit number, the maximum amount of information is 36 of the 5, 36^5 = 60466176, that is, the largest 5 digit number equals 10 digits: 60466176.
In this article in order to do a demo, we assume that a club issued a number of 10-bit membership card numbers, membership card number by 3-digit city number + 5-digit card number code + 2-bit check code composition. City number with the area code, such as 755 on behalf of Shenzhen, 5-bit card number is composed of 36 card number, the back two-bit check code is generated by a certain algorithm, the use of parity check code can verify the legality of the card number. In this way, our 10-bit card number is equivalent to the maximum of more than 60 million membership card number, and is not duplicate unique card number.
PHP implementation
We use PHP to make the conversion, 10 to 36 into the system.
Copy Code code as follows:
Class Code {
Password dictionary
Private $dic = Array (
0=> ' 0 ', 1=> ' 1 ', 2=> ' 2 ', 3=> ' 3 ', 4=> ' 4 ', 5=> ' 5 ', 6=> ' 6 ', 7=> ' 7 ', 8=> ' 8 ',
9=> ' 9 ', 10=> ' A ', 11=> ' B ', 12=> ' C ', 13=> ' D ', 14=> ' E ', 15=> ' F ', 16=> ' G ', 17=> ' H ',
18=> ' I ',19=> ' J ', 20=> ' K ', 21=> ' L ', 22=> ' M ', 23=> ' N ', 24=> ' O ', 25=> ' P ', 26=> ' Q ',
27=> ' R ',28=> ' S ', 29=> ' T ', 30=> ' U ', 31=> ' V ', 32=> ' W ', 33=> ' X ', 34=> ' Y ', 35=> ' Z '
);
Public Function Encodeid ($int, $format =8) {
$dics = $this->dic;
$dnum = 36; Number of systems in
$arr = Array ();
$loop = true;
while ($loop) {
$arr [] = $dics [Bcmod ($int, $dnum)];
$int = Bcdiv ($int, $dnum, 0);
if ($int = = ' 0 ') {
$loop = false;
}
}
if (count ($arr) < $format)
$arr = Array_pad ($arr, $format, $dics [0]);
Return implode (', Array_reverse ($arr));
}
Public Function Decodeid ($ids) {
$dics = $this->dic;
$dnum = 36; Number of systems in
Key value Exchange
$dedic = Array_flip ($dics);
Go to 0
$id = LTrim ($ids, $dics [0]);
Reverse
$id = Strrev ($id);
$v = 0;
for ($i = 0, $j = strlen ($id); $i < $j; $i + +) {
$v = Bcadd (Bcmul ($dedic [$id {
$i}
], Bcpow ($dnum, $i, 0), 0), $v, 0);
}
return $v;
}
}
We define the code class, first defines the cipher dictionary, namely the 0-z corresponding numeric value, the method Encodeid ($int, $format) The parameter $int represents the number, $format represents the bit length, for example Encodeid (123456789,5) Represents the conversion of the number 123456789 to a 5-bit 36-digit number, and method Decodeid ($ids) is used to convert a 36-to-numbered number to a 10-in-system number.
We can do this to generate the card number:
Copy Code code as follows:
$code = new Code ();
$card _no = $code->encodeid (888888,5);
As above, we can get a 5-bit card number, which actually represents the card number is 888888 (6 8) of the member number, and the actual conversion is the 5-bit number: 0J1VC.
Next, we add the city number and the check code, the city number is already defined, the checksum code is obtained by a certain algorithm, in this case, we use the simple algorithm: the first three-digit city number and five-bit card number for MD5 encryption, and then take MD5 value of the first 2 bits as a checksum code, This gets the two-digit checksum code after the number.
Copy Code code as follows:
$card _pre = ' 755 ';
$card _VC = substr (MD5 ($card _pre. $card _no), 0,2);
$card _VC = strtoupper ($card _VC);
Echo $card _pre. $card _no. $card _VC;
In practical application, we can get 10 numbers through the database, guarantee the unique number, then combine the above code, and finally generate a 10-bit repeat card number.