Generate Chinese characters randomly (extract saved code) and extract Chinese Characters

Source: Internet
Author: User

Generate Chinese characters randomly (extract saved code) and extract Chinese Characters

/// <Summary>
/// Generate Chinese characters randomly
/// </Summary>
/// <Param name = "strlength"> length (4 digits) </param>
/// <Returns> </returns>
Public string CreateCode (int strlength)
{
// Define a string array to store the components of Chinese character encoding
String [] r = 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];
/**/
/* Generate a hexadecimal byte array containing two elements at a time in each loop, and put 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 = r [r1]. Trim ();
// Code 2nd bits
Rnd = new Random (r1 * unchecked (int) DateTime. Now. Ticks) + I); // Replace the seed of the Random number generator to avoid repeated values.
Int r2;
If (r1 = 13)
R2 = rnd. Next (0, 7 );
Else
R2 = rnd. Next (0, 16 );
String str_r2 = r [r2]. Trim ();
// Code 3rd bits
Rnd = new Random (r2 * unchecked (int) DateTime. Now. Ticks) + I );
Int r3 = rnd. Next (10, 16 );
String str_r3 = r [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 = r [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 );
}


// Obtain the GB2312 encoding page (table)
Encoding gb = Encoding. GetEncoding ("gb2312 ");

// 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 []);
String txt = str1 + str2 + str3 + str4;


Return txt;

}

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.