中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

java生成漢字驗證碼

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

常常在輸入驗證碼時,都是簡單的字母+ 數(shù)字,而漢字則相對較少。 相關(guān)知識,漢字編碼原理

漢字驗證碼

import java.util.*;

public class CheckCode {
    public static void main(String[] args) {

        String code = "";

        Random random = new Random();

        String[] rBase = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                "a", "b", "c", "d", "e", "f" };
        // 生成第1位的區(qū)碼
        int r1 = random.nextInt(3) + 11; // 生成11到14之間的隨機數(shù)
        String str_r1 = rBase[r1];
        // 生成第2位的區(qū)碼
        int r2;
        if (r1 == 13) {
            r2 = random.nextInt(7); // 生成0到7之間的隨機數(shù)
        } else {
            r2 = random.nextInt(16); // 生成0到16之間的隨機數(shù)
        }
        String str_r2 = rBase[r2];
        // 生成第1位的位碼
        int r3 = random.nextInt(6) + 10; // 生成10到16之間的隨機數(shù)
        String str_r3 = rBase[r3];
        // 生成第2位的位碼
        int r4;
        if (r3 == 10) {
            r4 = random.nextInt(15) + 1; // 生成1到16之間的隨機數(shù)
        } else if (r3 == 15) {
            r4 = random.nextInt(15); // 生成0到15之間的隨機數(shù)
        } else {
            r4 = random.nextInt(16); // 生成0到16之間的隨機數(shù)
        }
        String str_r4 = rBase[r4];
        System.out.println(str_r1 + str_r2 + str_r3 + str_r4);

        // 將生成機內(nèi)碼轉(zhuǎn)換為漢字
        byte[] bytes = new byte[2];
        // 將生成的區(qū)碼保存到字節(jié)數(shù)組的第1個元素中
        String str_r12 = str_r1 + str_r2;
        int tempLow = Integer.parseInt(str_r12, 16);
        bytes[0] = (byte) tempLow;
        // 將生成的位碼保存到字節(jié)數(shù)組的第2個元素中
        String str_r34 = str_r3 + str_r4;
        int tempHigh = Integer.parseInt(str_r34, 16);
        bytes[1] = (byte) tempHigh;

        code = new String(bytes); // 根據(jù)字節(jié)數(shù)組生成漢字
        System.out.println("生成漢字:" + code);
    }
}

標簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇: JDBC的工具類

下一篇:java插入排序算法