C # Generate Promotion invitation code

Source: Internet
Author: User

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;
 }
    }
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.