C _ generate a random Chinese Character Verification Code

Source: Internet
Author: User

Using system;
Using system. text;

Namespace consoleapplication
{
Class chinesecode
{
Public static void main ()
{
// Obtain the gb2312 encoding page (table)
Encoding GB = encoding. getencoding ("gb2312 ");

 

// Call the function to generate four random Chinese character codes
Object [] bytes = createregioncode (4 );

// Decodes Chinese Characters Based on the byte array encoded by Chinese Characters
String str1 = GB. getstring (byte []) convert. changetype (Bytes [0], typeof (byte []);
String str2 = GB. getstring (byte []) convert. changetype (Bytes [1], typeof (byte []);
String str3 = GB. getstring (byte []) convert. changetype (Bytes [2], typeof (byte []);
String str4 = GB. getstring (byte []) convert. changetype (Bytes [3], typeof (byte []);

// Output Console
Console. writeline (str1 + str2 + str3 + str4 );
}

/**//*
This function randomly creates a hexadecimal byte array containing two elements within the Chinese character encoding range. Each byte array represents a Chinese character and
The four byte arrays are stored in the object array.
Parameter: strlength, indicating the number of Chinese characters to be generated
*/
Public static object [] createregioncode (INT strlength)
{
// Define a string array to store the components of Chinese character encoding
String [] rbase = new string [16] {"0", "1", "2", "3", "4", "5", "6 ", "7", "8", "9", "A", "B", "C", "D", "E", "F "};

Random RND = new random ();

// Define an object array
Object [] bytes = new object [strlength];

/** // * Generates a hexadecimal byte array containing two elements at a time in each loop, and puts it into the bject array.
Each Chinese Character consists of four location codes.
The first element of the byte array is the 1st bits and 2nd bits.
The second element of the byte array is the 3rd bits and 4th bits.
*/
For (INT I = 0; I <strlength; I ++)
{
// Code 1st bits
Int R1 = RND. Next (11,14 );
String str_r1 = rbase [R1]. Trim ();

// Code 2nd bits
RND = new random (R1 * unchecked (INT) datetime. Now. ticks) + I); // Replace

Seed to avoid repeated values
Int R2;
If (r1 = 13)
{
R2 = RND. Next (0, 7 );
}
Else
{
R2 = RND. Next (0, 16 );
}
String str_r2 = rbase [R2]. Trim ();

// Code 3rd bits
RND = new random (R2 * unchecked (INT) datetime. Now. ticks) + I );
Int R3 = RND. Next (10, 16 );
String str_r3 = rbase [R3]. Trim ();

// Code 4th bits
RND = new random (R3 * unchecked (INT) datetime. Now. ticks) + I );
Int R4;
If (R3 = 10)
{
R4 = RND. Next (1, 16 );
}
Else if (R3 = 15)
{
R4 = RND. Next (0, 15 );
}
Else
{
R4 = RND. Next (0, 16 );
}
String str_r4 = rbase [R4]. Trim ();

// Define the random Chinese character location code generated by storing two byte Variables
Byte byte1 = convert. tobyte (str_r1 + str_r2, 16 );
Byte byte2 = convert. tobyte (str_r3 + str_r4, 16 );
// Store two byte variables in the byte array
Byte [] str_r = new byte [] {byte1, byte2 };

// Put the byte array of the generated Chinese character into the object Array
Bytes. setvalue (str_r, I );

}

Return bytes;

}
}

}

 

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.