Java GUID 到 ByteArray
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2983065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GUID to ByteArray
提问by Chris Dutrow
I just wrote this code to convert a GUID into a byte array. Can anyone shoot any holes in it or suggest something better?
我只是写了这段代码来将 GUID 转换为字节数组。任何人都可以在其中打孔或提出更好的建议吗?
public static byte[] getGuidAsByteArray(){
UUID uuid = UUID.randomUUID();
long longOne = uuid.getMostSignificantBits();
long longTwo = uuid.getLeastSignificantBits();
return new byte[] {
(byte)(longOne >>> 56),
(byte)(longOne >>> 48),
(byte)(longOne >>> 40),
(byte)(longOne >>> 32),
(byte)(longOne >>> 24),
(byte)(longOne >>> 16),
(byte)(longOne >>> 8),
(byte) longOne,
(byte)(longTwo >>> 56),
(byte)(longTwo >>> 48),
(byte)(longTwo >>> 40),
(byte)(longTwo >>> 32),
(byte)(longTwo >>> 24),
(byte)(longTwo >>> 16),
(byte)(longTwo >>> 8),
(byte) longTwo
};
}
In C++, I remember being able to do this, but I guess theres no way to do it in Java with the memory management and all?:
在 C++ 中,我记得能够做到这一点,但我想在 Java 中没有办法通过内存管理来做到这一点吗?:
UUID uuid = UUID.randomUUID();
long[] longArray = new long[2];
longArray[0] = uuid.getMostSignificantBits();
longArray[1] = uuid.getLeastSignificantBits();
byte[] byteArray = (byte[])longArray;
return byteArray;
Edit
编辑
If you want to generate a completely random UUID as bytes that does not conform to any of the official types, this will work and wastes 10 fewer bitsthan type 4 UUIDs generated by UUID.randomUUID():
如果你想生成一个完全随机的 UUID 作为不符合任何官方类型的字节,这将有效并且比 UUID.randomUUID() 生成的类型 4 UUID少浪费10 位:
public static byte[] getUuidAsBytes(){
int size = 16;
byte[] bytes = new byte[size];
new Random().nextBytes(bytes);
return bytes;
}
采纳答案by aioobe
I would rely on built in functionality:
我会依赖内置功能:
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
return bb.array();
or something like,
或类似的东西,
ByteArrayOutputStream ba = new ByteArrayOutputStream(16);
DataOutputStream da = new DataOutputStream(ba);
da.writeLong(uuid.getMostSignificantBits());
da.writeLong(uuid.getLeastSignificantBits());
return ba.toByteArray();
(Note, untested code!)
(注意,未经测试的代码!)
回答by Bozho
You can check UUID
from apache-commons. You may not want to use it, but check the sourcesto see how its getRawBytes()
method is implemented:
您可以UUID
从 apache-commons 中查看。您可能不想使用它,但请查看来源以了解其getRawBytes()
方法是如何实现的:
public UUID(long mostSignificant, long leastSignificant) {
rawBytes = Bytes.append(Bytes.toBytes(mostSignificant), Bytes.toBytes(leastSignificant));
}
回答by comonad
public static byte[] newUUID() {
UUID uuid = UUID.randomUUID();
long hi = uuid.getMostSignificantBits();
long lo = uuid.getLeastSignificantBits();
return ByteBuffer.allocate(16).putLong(hi).putLong(lo).array();
}
回答by Kurt Vangraefschepe
You could take a look at Apache Commons Lang3 Conversion.uuidToByteArray(...). Conversely, look at Conversion.byteArrayToUuid(...)to convert back to a UUID.
你可以看看 Apache Commons Lang3 Conversion.uuidToByteArray(...)。相反,查看Conversion.byteArrayToUuid(...)以转换回 UUID。