GUID 到 ByteArray
我刚刚编写了这段代码来将 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
};
}
在 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;
编辑
如果你想生成一个完全随机的 UUID 作为不符合任何的字节官方类型,这将起作用,并且比 UUID.randomUUID() 生成的类型 4 UUID 少10 位:
public static byte[] getUuidAsBytes(){
int size = 16;
byte[] bytes = new byte[size];
new Random().nextBytes(bytes);
return bytes;
}
I just wrote this code to convert a GUID into a byte array. Can anyone shoot any holes in it or suggest something better?
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?:
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 bits than type 4 UUIDs generated by UUID.randomUUID():
public static byte[] getUuidAsBytes(){
int size = 16;
byte[] bytes = new byte[size];
new Random().nextBytes(bytes);
return bytes;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会依赖内置功能:
或类似的东西
(注意,未经测试的代码!)
I would rely on built in functionality:
or something like,
(Note, untested code!)
您可以检查
UUID< /code>
来自 apache-commons。您可能不想使用它,但请检查 sources 查看其
getRawBytes()
方法是如何实现的:You can check
UUID
from apache-commons. You may not want to use it, but check the sources to see how itsgetRawBytes()
method is implemented:您可以查看 Apache Commons Lang3 Conversion.uuidToByteArray(...)。相反,请查看 Conversion.byteArrayToUuid(...) 转换回 UUID。
You could take a look at Apache Commons Lang3 Conversion.uuidToByteArray(...). Conversely, look at Conversion.byteArrayToUuid(...) to convert back to a UUID.