0

我想生成类似于 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() 我不确定哪种类型是合适的。任何想法?

4

1 回答 1

1
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();
}
于 2013-05-16T12:39:09.277 回答