C# 隨機生成中文字符串

  1 using System;
  2 using System.Text;
  3 
  4 <font color=red>
  5 /// 能夠隨機生成一個長度爲2的十六進制字節數組,
  6 /// 使用GetString ()方法對其進行解碼就能夠獲得漢字字符了。
  7 /// 不過對於生成中文漢字驗證碼來講,由於第15區也就是AF區之前都沒有漢字,
  8 /// 只有少許符號,漢字都從第16區B0開始,而且從區位D7開始之後的漢字都是和很難見到的繁雜漢字,
  9 /// 因此這些都要排出掉。因此隨機生成的漢字十六進制區位碼第1位範圍在B、C、D之間,
 10 /// 若是第1位是D的話,第2位區位碼就不能是7之後的十六進制數。
 11 /// 在來看看區位碼錶發現每區的第一個位置和最後一個位置都是空的,沒有漢字,
 12 /// 所以隨機生成的區位碼第3位若是是A的話,第4位就不能是0;第3位若是是F的話,
 13 /// 第4位就不能是F。知道了原理,隨機生成中文漢字的程序也就出來了, 
 14 
 15 /// 如下就是生成長度爲N的隨機漢字C#臺代碼:
 16 </font>
 17 public class RandomChinese
 18 {
 19     public RandomChinese()
 20     {
 21     }
 22 
 23     public string GetRandomChinese(int strlength)
 24     {
 25         // 獲取GB2312編碼頁(表) 
 26         Encoding gb = Encoding.GetEncoding("gb2312");
 27 
 28         object[] bytes = this.CreateRegionCode(strlength);
 29 
 30         StringBuilder sb = new StringBuilder();
 31 
 32         for (int i = 0; i < strlength; i++)
 33         {
 34             string temp = gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[])));
 35             sb.Append(temp);
 36         }
 37 
 38         return sb.ToString();
 39     }
 40 
 41     /** 
 42     此函數在漢字編碼範圍內隨機建立含兩個元素的十六進制字節數組,每一個字節數組表明一個漢字,並將 
 43     四個字節數組存儲在object數組中。 
 44     參數:strlength,表明須要產生的漢字個數 
 45     **/    
 46     private object[] CreateRegionCode(int strlength)
 47     {
 48         //定義一個字符串數組儲存漢字編碼的組成元素 
 49         string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
 50 
 51         Random rnd = new Random();
 52 
 53         //定義一個object數組用來 
 54         object[] bytes = new object[strlength];
 55 
 56         /**
 57          每循環一次產生一個含兩個元素的十六進制字節數組,並將其放入bytes數組中 
 58          每一個漢字有四個區位碼組成 
 59          區位碼第1位和區位碼第2位做爲字節數組第一個元素 
 60          區位碼第3位和區位碼第4位做爲字節數組第二個元素 
 61         **/
 62         for (int i = 0; i < strlength; i++)
 63         {
 64             //區位碼第1位 
 65             int r1 = rnd.Next(11, 14);
 66             string str_r1 = rBase[r1].Trim();
 67 
 68             //區位碼第2位 
 69             rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i); // 更換隨機數發生器的 種子避免產生重複值 
 70             int r2;
 71             if (r1 == 13)
 72             {
 73                 r2 = rnd.Next(0, 7);
 74             }
 75             else
 76             {
 77                 r2 = rnd.Next(0, 16);
 78             }
 79             string str_r2 = rBase[r2].Trim();
 80 
 81             //區位碼第3位 
 82             rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
 83             int r3 = rnd.Next(10, 16);
 84             string str_r3 = rBase[r3].Trim();
 85 
 86             //區位碼第4位 
 87             rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
 88             int r4;
 89             if (r3 == 10)
 90             {
 91                 r4 = rnd.Next(1, 16);
 92             }
 93             else if (r3 == 15)
 94             {
 95                 r4 = rnd.Next(0, 15);
 96             }
 97             else
 98             {
 99                 r4 = rnd.Next(0, 16);
100             }
101             string str_r4 = rBase[r4].Trim();
102 
103             // 定義兩個字節變量存儲產生的隨機漢字區位碼 
104             byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
105             byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
106             // 將兩個字節變量存儲在字節數組中 
107             byte[] str_r = new byte[] { byte1, byte2 };
108 
109             // 將產生的一個漢字的字節數組放入object數組中 
110             bytes.SetValue(str_r, i);
111         }
112 
113         return bytes;
114     }
115 }

轉自:http://blog.csdn.net/xjj51296646/article/details/3928428數組

相關文章
相關標籤/搜索