簡體   English   中英

Java BitConverter等價

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM