Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 十六进制格式的字节值并将其写入字节流?_Java - Fatal编程技术网

Java 十六进制格式的字节值并将其写入字节流?

Java 十六进制格式的字节值并将其写入字节流?,java,Java,获取十六进制格式的字节值序列并将其写入字节流。 “01 02 1a”=>将字节0x01 0x02 0x1a写入字节流 这意味着什么?这里有一个可能的解决方案: String hex = "01 02 1a"; // Remove spaces hex = hex.replace(" ", ""); // Array containing bytes byte[] bytes = new byte[hex.length() / 2]; int

获取十六进制格式的字节值序列并将其写入字节流。 “01 02 1a”=>将字节0x01 0x02 0x1a写入字节流


这意味着什么?

这里有一个可能的解决方案:

    String hex = "01 02 1a";

    // Remove spaces
    hex = hex.replace(" ", "");

    // Array containing bytes
    byte[] bytes = new byte[hex.length() / 2];

    int k = 0;
    for(int i=0; i < hex.length(); i = i +2 ) {
        // Read and parse each byte
        int b = Integer.parseInt(hex.substring(i, i + 2), 16);
        bytes[k] = (byte) b;
        k++;
    }

    // Write bytes to an outputStram
    OutputStream out;
    for(Byte b: bytes) {
        out.write(b);
    }
字符串hex=“01 02 1a”; //删除空格 十六进制=十六进制。替换(“,”); //包含字节的数组 byte[]bytes=新字节[hex.length()/2]; int k=0; 对于(int i=0;i0x01 0x02 0x1a这应该是outputYes,这意味着您必须将每个字节写入outputStream:
for(byte b:bytes)outputStream.write(b)我将其添加到我的帖子中。“十六进制格式的字节值序列”,这是十六进制格式的字节值字符串还是字节数组?“写入字节流”,您指的是文件吗?十六进制格式字节值字符串“01、02、1a”您的字节流是什么?您是使用FileOutputStream将结果写入文件中,还是希望将其以纯文本形式写入以显示0x01 0x02 0x1a?我这样问是因为文件中的实际字节值在值之前不会有“0x”。它应该添加到ByteBuffer中。在ByteBuffer中不会看到0x01 0x02 0x1a。请更具体地说明你想要完成什么。