隨機生成指定個數的漢字
阿新 • • 發佈:2019-02-14
using UnityEngine; using System.Collections; using System.Text; public class GenerateChineseWords { /// /// 隨機產生常用漢字 /// /// 要產生漢字的個數 /// 常用漢字 public static string GenerateChineseWord(int count) { string chineseWords = ""; System.Random rm = new System.Random(); Encoding gb = Encoding.GetEncoding("gb2312"); for (int i = 0; i < count; i++) { // 獲取區碼(常用漢字的區碼範圍為16-55) int regionCode = rm.Next(16, 56); // 獲取位碼(位碼範圍為1-94 由於55區的90,91,92,93,94為空,故將其排除) int positionCode; if (regionCode == 55) { // 55區排除90,91,92,93,94 positionCode = rm.Next(1, 90); } else { positionCode = rm.Next(1, 95); } // 轉換區位碼為機內碼 int regionCode_Machine = regionCode + 160;// 160即為十六進位制的20H+80H=A0H int positionCode_Machine = positionCode + 160;// 160即為十六進位制的20H+80H=A0H // 轉換為漢字 byte[] bytes = new byte[] { (byte)regionCode_Machine, (byte)positionCode_Machine }; chineseWords += gb.GetString(bytes); } return chineseWords; } }