[英]Java BitConverter equivalent
在C#中使用以下塊
using (var nuq = new RNGCryptoServiceProvider())
{
var data = new byte[4];
nuq.GetBytes(data);
return BitConverter.ToUInt32(data, 0).ToString(CultureInfo.InvariantCulture);
}
我想用Java轉換它。 到目前為止,我有:
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
var data = new byte[4];
random.nextBytes(data);
我不知道BitConverter
的Java等價物。
如何將數據轉換為UInt32
?
獲得隨機數后,您可以返回一個將字節轉換為long
的方法的調用,如:
public long bytesToLong(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.put(bytes);
buffer.flip();//need flip
return buffer.getLong();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.