关于C#小程序都问题。

来源:百度知道 编辑:UC知道 时间:2024/09/26 02:20:28
怎么判断一个ascii码值所对应的数,还有就是随即数怎么能数字字母一起产生。

一楼没有说如何转换ASCII码 我给你方法出来你看看

private void RandomCode()
{
Random rand = new Random();
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
int i = 1;
byte[] byteArray = new byte[] { (byte)0 };
while (i <= 10)
{
int x = rand.Next(25);
byteArray = new byte[] { (byte)(x + 97) };
code += asciiEncoding.GetString(byteArray) + x;
i++;
}
//code的值为 产生的包含数字和小写字母的随即码
}

首先你要知道在ascii码中 97代表 "a"

您好,您可以尝试将char强制转换成int,就可以看到相应的内容。
另外随机数数字字母一起产生则产生的是一个字符串,可以参考验证码的生成方式,代码如下:
public static string CreateRandomCode(int codeCount)
{
string allChar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";
string[] allCharArray = allChar.Split(',');