We will use 0-Z (0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ) to represent the values 0-35, such as the letter Z represents 35. So I want to get a five numbers, the greatest amount of information is 36 5 th, and 36 ^ 5 = 60,466,176, which is equivalent to the maximum number of five decimal numbers: 60466176.
In order to do presentations in this article, we assume that a group of 10 paid a club membership number, membership number by three city ID card encoding +2 +5 bit checksum components. City number with area code indicated as 755 representatives of Shenzhen, five card numbers from card number consisting of 36 hexadecimal, followed by two check code is generated by a certain algorithm is able to use the checksum to verify the card number legitimacy. In this case, we generate 10 card number corresponds to the maximum to meet the 60 million membership number, and is not repeated unique card number.
PHP
We use PHP to convert hexadecimal, decimal turn 36 hex.
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; //Hexadecimal number
$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; //Hexadecimal number
//Key exchange
$dedic = array_flip($dics);
//Go to zero
$id = ltrim($ids, $dics[0]);
//trun
$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, define a password dictionary, namely 0-Z corresponding numerical method encodeID ($ int, $ format) in the parameter $ int represents a number, $ format represents the median length, for example encodeID (123456789,5) expressed will be converted into five figures 123456789 36 hexadecimal numbers and methods decodeID ($ ids) is used to convert 36 hex number 10 hex number.
We can do to generate the card:
$code = new Code();
$card_no = $code->encodeID(888888,5);
As above, we can get a five card number, it actually represents the card number is 888888 (6 8), membership number, but the actual conversion is five numbers: 0J1VC.
Next, we check the number and the city code plus the city ID is already defined, the check code through a certain algorithm to obtain, in this case, we use a simple algorithm: The first three numbers and five cities md5 card numbers were encrypted, and then take the first two as md5 checksum value, so get the numbers behind the two checksum.
$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;
Practical applications, you can get 10 hexadecimal numbers through a database to ensure that Unique numbers, then use the above code combinations, and ultimately generate a 10-bit card number is not repeated.