在JAVA中生成UUID字符串的有效方法(不带破折号的UUID.randomUUID()。toString())
最终基于UUID.java实现编写了自己的东西。请注意,我 ,而是以我能想到的最有效的方式 随机的32字节十六进制字符串。
实作import java.security.SecureRandom;import java.util.UUID;public class RandomUtil { // Maxim: copied from UUID implementation :) private static volatile SecureRandom numberGenerator = null; private static final long MSB = 0x8000000000000000L; public static String unique() {SecureRandom ng = numberGenerator;if (ng == null) { numberGenerator = ng = new SecureRandom();}return Long.toHexString(MSB | ng.nextLong()) + Long.toHexString(MSB | ng.nextLong()); } }用法
RandomUtil.unique()测验
我已经测试过一些输入,以确保其正常工作:
public static void main(String[] args) { System.out.println(UUID.randomUUID().toString()); System.out.println(RandomUtil.unique()); System.out.println(); System.out.println(Long.toHexString(0x8000000000000000L |21)); System.out.println(Long.toBinaryString(0x8000000000000000L |21)); System.out.println(Long.toHexString(Long.MAX_VALUE + 1));}解决方法
我想要一个高效的实用程序来生成唯一的字节序列。UUID是一个很好的候选人,但是会UUID.randomUUID().toString()生成类似的东西44e128a5-ac7a-4c9a-be4c-224b6bf81b20,但是我更喜欢无破折号的字符串。
我正在寻找一种仅从字母数字字符(无破折号或任何其他特殊符号)生成随机字符串的有效方法。
相关文章:
1. java - Activity 进入后台再次传值?2. 前端 - flex布局采用space-around这种方法,但是最后一行如何让他左对齐?3. 前端 - 使用两个transtion只有一个生效?4. chrome - linux系统下如何通过java获取客户端ip和mac地址?5. javascript - sublime已经安装了babel插件和sublimelinter-jshint为什么还是显示es6语法错误?6. angular.js - ng-include 会缓存html吗?7. css - 手机app中rem的基准值计算错误8. 前端 - 在webstrom上pull代码时出现错误?9. 小白问题getDay()10. java - git项目迁移到SVN怎么实现的?哪位大神指点指点

网公网安备