Requirements Description
1 Generate a unique invitation code according to the user ID;
2) The user ID is pushed back according to the invitation code;
Main steps
1 Set the invitation code number Len, custom random sequence string sourcecode, user ID;
Note: The random sequence string increases its security, excluding the complement "0" and the confusing "i,o" in SourceCode;
2 The user ID is converted into 16;
3) to the converted string high up to complement 0;
4 the interval inserts the random character;
Code implementation
Using System; Namespace Invitedcode {class Program {static void Main (string[] args) {int len = 10 ; Code even digit//custom scrambled string, ' 0-9-A-Z ', excluding easily confused characters ' i,o ', excluding high-level complement ' 0 ' string sourcecode = ' Mwx89fcdg3j2rs1tuklyze5
V67HQA4BNP "; int uid = 12345;
User ID String code = Createcode (Len,sourcecode, UID);
String decode = decode (code);
Console.WriteLine (code);
Console.WriteLine (decode);
Console.readkey ();
private static string Decode (string code) {//extract user ID string str = "";
String uid = ""; for (int i = 0; i < code. Length; i + + 2) {str = code.
Substring (i, 1); //Remove high 0 for (int i = 0; i < str.) Length; i++) {if (!STR). Substring (i, 1). Equals ("0")) {str = str. Substring (i, str.)
LENGTH-I);
Break The UID = Convert.ToInt32 (str, 16).
ToString ();
return UID;
private static string Createcode (int len, string sourcecode, int uid) {//decimal turn hexadecimal
String hexid = Convert.ToString (uid,16);
High up 0 String str = ""; for (int i = LEN/2 i > Hexid.) Length;
i--) {str = "0";
STR + + Hexid;
Insert Random character Random ran = new Random (); for (int i = 1; i < Str. length+1; i + 2) {str = str. Insert (i, sourcecode. Substring (ran. Next (0, SourceCode.
Length-1), 1);
return str;
}
}
}