在JAVA中生成UUID字符串的有效方法(不帶破折號的UUID.randomUUID()。toString())
最終基于UUID.java實現(xiàn)編寫了自己的東西。請注意,我 ,而是以我能想到的最有效的方式 隨機的32字節(jié)十六進制字符串。
實作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()測驗
我已經(jīng)測試過一些輸入,以確保其正常工作:
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));}解決方法
我想要一個高效的實用程序來生成唯一的字節(jié)序列。UUID是一個很好的候選人,但是會UUID.randomUUID().toString()生成類似的東西44e128a5-ac7a-4c9a-be4c-224b6bf81b20,但是我更喜歡無破折號的字符串。
我正在尋找一種僅從字母數(shù)字字符(無破折號或任何其他特殊符號)生成隨機字符串的有效方法。
相關(guān)文章:
1. docker網(wǎng)絡(luò)端口映射,沒有方便點的操作方法么?2. debian - docker依賴的aufs-tools源碼哪里可以找到啊?3. 前端 - ng-view不能加載進模板4. 關(guān)docker hub上有些鏡像的tag被標記““This image has vulnerabilities””5. python - from ..xxxx import xxxx到底是什么意思呢?6. angular.js - angularJS在Android WebView中無法正常調(diào)后臺接口7. nignx - docker內(nèi)nginx 80端口被占用8. docker容器呢SSH為什么連不通呢?9. 請教各位大佬,瀏覽器點 提交實例為什么沒有反應(yīng)10. ddos - apache日志很多其它網(wǎng)址,什么情況?
