在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
发布于 2015-02-16 20:13:26
当您获得随机数时,您可以返回对将字节转换为long的方法的调用,如下所示:
public long bytesToLong(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.put(bytes);
buffer.flip();//need flip
return buffer.getLong();
}https://stackoverflow.com/questions/28538970
复制相似问题