gpt4 book ai didi

java - 生成随机二进制数据,例如 23ABFF

转载 作者:行者123 更新时间:2023-12-01 14:34:43 24 4
gpt4 key购买 nike

我想生成类似于 OData 二进制的二进制数据,但我不知道如何生成。类型定义为

Represent fixed- or variable- length binary data
binary'[A-Fa-f0-9][A-Fa-f0-9]*' OR X '[A-Fa-f0-9][A-Fa-f0-9]*' NOTE: X and binary are case sensitive. Spaces are not allowed between binary and the quoted portion. Spaces are not allowed between X and the quoted portion. Odd pairs of hex digits are not allowed.

**Example 1: X'23AB' Example 2: binary'23ABFF'**

对于 next.random() 我不确定哪种类型合适。有什么想法吗?

最佳答案

new Random().nextBytes(byte[])

编辑:您还可以通过以下方式实现此目的

new Random().nextInt(16)

参见:

int nbDigitsYouWant=8;
Random r=new Random();
for(int i=0;i<nbDigitsYouWant;i++){
//display hexa representation
System.out.print(String.format("%x",r.nextInt(16)));
}

输出:

ea0d3b9d

编辑:这是一个快速但肮脏的示例,其中随机字节发送到 DataOutputStream。

public static void main(String[] args) throws Exception{
DataOutputStream dos=new DataOutputStream(new FileOutputStream("/path/to/your/file"));

int nbDesiredBytes=99999999;
int bufferSize=1024;
byte[] buffer = new byte[bufferSize];
Random r=new Random();

int nbBytes=0;
while(nbBytes<nbDesiredBytes){
int nbBytesToWrite=Math.min(nbDesiredBytes-nbBytes,bufferSize);
byte[] bytes=new byte[nbBytesToWrite];
r.nextBytes(bytes);
dos.write(bytes);
nbBytes+=nbBytesToWrite;
}

dos.close();
}

关于java - 生成随机二进制数据,例如 23ABFF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16587559/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com