Generating Chinese character Verification code in C #

Source: Internet
Author: User
Tags trim
Program code:



Using System;
Using System.Text;

Namespace Consoleapplication
{
Class Chinesecode
{
public static void Main ()
{
Get GB2312 Encoding page (table)
Encoding gb=encoding.getencoding ("gb2312");

Call function generates 4 random Chinese encoding
Object[] Bytes=createregioncode (4);

Decoding Chinese characters according to the encoding byte array
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[)));

Console for output
Console.WriteLine (str1 + str2 +str3 +STR4);
}


/**//*
This function randomly creates a hexadecimal byte array with two elements in the range of encoding, each byte array represents a character, and the
A four-byte array is stored in an object array.
Parameters: Strlength, representing the number of characters to be produced
*/
public static object[] Createregioncode (int strlength)
{
Defines a string array that stores the constituent elements of the 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 ();

Defines an object array to
Object[] Bytes=new object[strlength];

/**//* produces a hexadecimal byte array of two elements at a time, and puts it in a bject array
Each Chinese character is composed of four location codes.
Location Code 1th and location code 2nd bit as the first element of a byte array
Location Code 3rd and Location Code 4th digits as the second element of a byte array
*/
for (int i=0;i<strlength;i++)
{
Location Code 1th Place
int R1=rnd. Next (11,14);
String STR_R1=RBASE[R1]. Trim ();

Location Code 2nd place
Rnd=new Random (r1*unchecked (int) DateTime.Now.Ticks) +i);//replace the random number generator

Seeds Avoid duplicate values
int R2;
if (r1==13)
{
R2=rnd. Next (0,7);
}
Else
{
R2=rnd. Next (0,16);
}
String STR_R2=RBASE[R2]. Trim ();

Location Code 3rd place
Rnd=new Random (r2*unchecked (int) DateTime.Now.Ticks) +i);
int R3=rnd. Next (10,16);
String STR_R3=RBASE[R3]. Trim ();

Location Code 4th place
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 ();

Defines a random Chinese character location code generated by two byte variable storage
Byte Byte1=convert.tobyte (str_r1 + str_r2,16);
Byte Byte2=convert.tobyte (STR_R3 + str_r4,16);
Storing two byte variables in a byte array
Byte[] Str_r=new Byte[]{byte1,byte2};

Puts a byte array of the resulting Chinese character into an object array
bytes. SetValue (Str_r,i);

}

return bytes;

}
}

}


After the random generation of Chinese characters is realized, the. NET GDI can be used to draw the verification code graphics that you need. How to generate the verification code picture, as well as changing the length and width of the characters in the Internet has been a lot of related articles, here due to the length of the article is no longer introduced. However, one thing to note is that the above code runs under the Chinese version of Windows, because it has a GB character set, and if you are an operating system in another language, you need to install the GB character set.
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.