简体   繁体   English

从java中的char数组和byte数组中提取某些字节

[英]Extract certain byte from char array and byte array in java

I have an char array containing hex value. 我有一个包含十六进制值的char数组。 It contains 6 bytes. 它包含6个字节。 I have calculated the crc of these 6 bytes and the function returns int value. 我已经计算出这6个字节的crc,并且该函数返回int值。 This is the code. 这是代码。

char buffer[] = {0x01,0x05,0x00,0x06,0x00,0x00};

byte[] bufferbyte = new String(buffer).getBytes();
for (byte bb : bufferbyte){
  System.out.format("0X%x ", bb);
}

int crcresult;
crcresult = CRC16(buffer,6); //crc calculation

byte[] crc_bytes = ByteBuffer.allocate(4).putInt(crcresult).array();

for (byte b : crc_bytes){
  System.out.format("0X%x ", b);
}

My question are 我的问题是

  1. I have used bytebuffer to convert crc obtained as int into byte. 我已使用字节缓冲区将以int形式获取的crc转换为字节。 But the calculated crc are stored in 4 byte instead of 2 byte. 但是计算的crc存储在4个字节中,而不是2个字节中。 I have calculated CRC 16 but the resulting crc is 32 bit . 我已经计算出CRC 16,但最终的crc为32位。 I think it is because i have returned "int" in the crc calculation and it is written that in java an int is 32 bits. 我认为这是因为我在crc计算中返回了“ int”,并且在Java中写为int是32位。

    So How to extract only two bytes from the byte buffer (crc_bytes) or the calculated int crc (crcresult). 因此,如何仅从字节缓冲区(crc_bytes)或计算出的int crc(crcresult)中提取两个字节。

  2. I have put the bytes of the "char buffer[]" and two bytes of calculated crc in single byte array. 我已经将“ char buffer []”的字节和两个计算出的crc字节放入了单字节数组中。 How can we append 我们如何追加

     char buffer[] and crcresult 

    in one byte array. 在一个字节数组中。

The output of above code is 上面代码的输出是

 0X1 0X5 0X0 0X6 0X0 0X0 0X0 0X0 0X2d 0Xcb 

Where first 6 bytes are bytes converted from char array and last 4 bytes are crc. 前6个字节是从char数组转换而来的字节,后4个字节是crc。

The two bytes of the crc in big endian order can be fetched with crc的两个字节(大尾数顺序)可以用

byte[] crc_result = new byte[2];
crc_bytes[0] = (byte)(crcresult >> 8); // this are the high order 8 bits
crc_bytes[1] = (byte)crcresult; // this are the low order 8 bits

If you need it in little endian order just adapt the assignments accordingly. 如果您需要按小端顺序排列,只需相应地调整分配即可。

It is not clear to me why you use a char array to represent bytes. 我不清楚为什么要使用char数组表示字节。

Yes, crcresult is 32 bits because it is of type int . 是的, crcresult为32位,因为它的类型为int If you want a 16bit data type, use short instead. 如果要使用16位数据类型,请改用short

But, using int type does not do any harm. 但是,使用int类型不会造成任何危害。 Although it is 32 bit, only last 16 bits will contain CRC16 value. 尽管它是32位,但只有最后16位将包含CRC16值。 You can extract those two bytes with following bitwise operations. 您可以使用以下按位运算来提取这两个字节。

byte byte1 = (byte)((crcresult >> 8) & 0xFF); // first 8 bits of last 16 bits
byte byte0 = (byte)(crcresult & 0xFF);        // last 8 bits

To merge the results. 合并结果。

byte[] merged = new byte[bufferbyte.length + 2];
System.arrayCopy(bufferbyte, 0, merged, 0, bufferbyte.length);  // copy original data buffer
merged[bufferbyte.length    ] = byte1;                      // append crc16 byte 1  
merged[bufferbyte.length + 1] = byte0;                      // append crc16 byte 2   

Refer System.arrayCopy for more details. 有关更多详细信息,请参考System.arrayCopy

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

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