如何将两个长字符转换为字节数组=如何将uid转换为字节数组?

时间:2022-12-06 21:15:37

I am using Javas UUID and need to convert a UUID to a byte Array. Strangely the UUID Class does not provide a "toBytes()" method.

我正在使用Javas UUID,需要将UUID转换为字节数组。奇怪的是,UUID类不提供“toBytes()”方法。

I already found out about the two methods:

我已经发现了这两种方法:

UUID.getMostSignificantBits()
and
UUID.getLeasSignificantBits()

But how to get this into a byte array? the result should be a byte[] with those tow values. I somehow need to do Bitshifting but, how?

但是如何将它转换成字节数组呢?结果应该是一个带有这两个值的字节[]。我需要做点改变,但是怎么做呢?

update:

更新:

I found:

我发现:

 ByteBuffer byteBuffer = MappedByteBuffer.allocate(2);
 byteBuffer.putLong(uuid.getMostSignificantBits());
 byteBuffer.putLong(uuid.getLeastSignificantBits());

Is this approach corret?

这种方法是正确的吗?

Are there any other methods (for learning purposes)?

是否有其他方法(用于学习目的)?

Thanks very much!! Jens

非常感谢! !延斯

3 个解决方案

#1


15  

You can use ByteBuffer

您可以使用ByteBuffer

 byte[] bytes = new byte[16];
 ByteBuffer bb = ByteBuffer.wrap(bytes);
 bb.order(ByteOrder.LITTLE_ENDIAN or ByteOrder.BIG_ENDIAN);
 bb.putLong(UUID.getMostSignificantBits());
 bb.putLong(UUID.getLeastSignificantBits());

 // to reverse
 bb.flip();
 UUID uuid = new UUID(bb.getLong(), bb.getLong());

#2


5  

One option if you prefer "regular" IO to NIO:

如果你喜欢“常规”IO而不是NIO,有一个选择:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.write(uuid.getMostSignificantBits());
dos.write(uuid.getLeastSignificantBits());
dos.flush(); // May not be necessary
byte[] data = dos.toByteArray();

#3


0  

For anyone trying to use this in Java 1.7, I found the following to be necessary:

<!-- language: lang-java -->
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeLong(password.getMostSignificantBits());
dos.writeLong(password.getLeastSignificantBits());
dos.flush(); // May not be necessary
return baos.toByteArray();

#1


15  

You can use ByteBuffer

您可以使用ByteBuffer

 byte[] bytes = new byte[16];
 ByteBuffer bb = ByteBuffer.wrap(bytes);
 bb.order(ByteOrder.LITTLE_ENDIAN or ByteOrder.BIG_ENDIAN);
 bb.putLong(UUID.getMostSignificantBits());
 bb.putLong(UUID.getLeastSignificantBits());

 // to reverse
 bb.flip();
 UUID uuid = new UUID(bb.getLong(), bb.getLong());

#2


5  

One option if you prefer "regular" IO to NIO:

如果你喜欢“常规”IO而不是NIO,有一个选择:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.write(uuid.getMostSignificantBits());
dos.write(uuid.getLeastSignificantBits());
dos.flush(); // May not be necessary
byte[] data = dos.toByteArray();

#3


0  

For anyone trying to use this in Java 1.7, I found the following to be necessary:

<!-- language: lang-java -->
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeLong(password.getMostSignificantBits());
dos.writeLong(password.getLeastSignificantBits());
dos.flush(); // May not be necessary
return baos.toByteArray();