繁体   English   中英

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

[英]byte values in hex format and writes these to the byte stream?

接受十六进制格式的字节值序列,并将其写入字节流。 “ 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);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM