栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java生成随机数,16进制字符串和二进制binary字符串

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java生成随机数,16进制字符串和二进制binary字符串

class ByteSumSt {
public ByteSumSt(byte[] bDest, int bDestLen) {
this.bDest = bDest;
this.bDestLen = bDestLen;
}
byte[] bDest;
int bDestLen;
}

public static ByteSumSt TransUtilsGenerateRandom(int randomLength, boolean isHex) {
    int capacityLength = isHex ? randomLength * 2 : randomLength;
    byte[] bRandom = new byte[capacityLength];
    for (int i = 0; i < randomLength; i++) {
        double dr = Math.random();
        byte rByte = (byte)(dr * 256);
        if (isHex) {
            String strHex = Integer.toHexString(rByte & 0x0FF);
            if (1 == strHex.length()) {
                strHex = "0" + strHex;
            }
            bRandom[i * 2] = strHex.getBytes(StandardCharsets.UTF_8)[0];
            bRandom[i * 2 + 1] = strHex.getBytes(StandardCharsets.UTF_8)[1];
        } else {
            bRandom[i] = rByte;
        }
    }
    return new ByteSumSt(bRandom, capacityLength);
}

使用
ByteSumSt randomKey = TransUtilsGenerateRandom(16, false);
ishex为true时输出的是十六进制表示的字符串

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/786258.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号