如何在Java中创建整数UUID

Adh*_*ham 2 java uuid integer

可能重复:
是否有一种方法可以生成仅由数字组成的随机UUID?

我不想在UUID唯一的整数中有字符..如何做到这一点java

cha*_*ite 5

您需要 2long秒来存储UUID.

UUID myuuid = UUID.randomUUID();
long highbits = myuuid.getMostSignificantBits();
long lowbits = myuuid.getLeastSignificantBits();
System.out.println("My UUID is: " + highbits + " " + lowbits);
Run Code Online (Sandbox Code Playgroud)

  • 这可能会有“-”符号。 (2认同)

cor*_*iKa 5

  • 使用getMostSigBits()getLeastSigBits()获取长值。
  • 然后使用这些长值填充byte[]
  • 然后使用该byte []创建一个BigInteger对象。
  • 也就是说BigInteger的toString()将是一个UUID可以潜在为负。您可以通过将-标志替换为1或其他类似方法来解决此问题。

我还没有测试过,但是#gimmetehcodez

long hi = id.getMostSignificantBits();
long lo = id.getLeastSignificantBits();
byte[] bytes = ByteBuffer.allocate(16).putLong(hi).putLong(lo).array();
BigInteger big = new BigInteger(bytes);
String numericUuid = big.toString().replace('-','1'); // just in case
Run Code Online (Sandbox Code Playgroud)

  • 你...你一定是在跟我开玩笑。这实际上使用了逐步说明,以及要调用的方法和所有内容。 (9认同)