隨機生成指定字數的簡體漢字
[Java]代碼
import java.io.UnsupportedEncodingException; import java.util.Random; /** * * @ClassName: ChineseUtil * @Description: 隨機生成漢字 * @author 王君義 * @date 2015-5-8 下午9:22:06 * */ public class ChineseUtil { /** * @Title: getChinese * @Description:隨機生成指定字數的簡體漢字 * @param len * @return String * */ public static String generateChinese(int len) { String ret = ""; for (int i = 0; i < len; i++) { String str = null; int hightPos, lowPos; // 定義高低位 Random random = new Random(); hightPos = (176 + Math.abs(random.nextInt(39))); //獲取高位值 lowPos = (161 + Math.abs(random.nextInt(93))); //獲取低位值 byte[] b = new byte[2]; b[0] = (new Integer(hightPos).byteValue()); b[1] = (new Integer(lowPos).byteValue()); try { str = new String(b, "GBk"); //轉成中文 } catch (UnsupportedEncodingException ex) { ex.printStackTrace(); } ret += str; } return ret; } }
本文由用戶 BeulahGibbs 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!