51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Java字节处理的一点经验

1、大小端处理;

小端填充整形变量,低位在前面,高位在后面
int pos = 0;
msg[pos++] =  (byte) (anchor & 0x00ff);
msg[pos++] =  (byte) ((anchor & 0xff00) >> 8);
msg[pos++] =  (byte) ((anchor & 0xff0000) >> 16);
msg[pos++] =  (byte) ((anchor & 0xff000000) >> 24);
大端填充: 
msg[pos++] = (byte)(term_id & 0xff000000 >> 24);
msg[pos++] = (byte)(term_id & 0xff0000 >> 16);
msg[pos++] = (byte)(term_id & 0xff00 >> 8);
msg[pos++] = (byte)(term_id & 0xff);


2、GBK处理

public static byte[] utf8ToGbk(String input) {
    Charset gb2312 = Charset.forName("GBK");
    byte[] utf8Bytes = input.getBytes(Charset.forName("UTF-8"));
    byte[] led1_data = new String(utf8Bytes, Charset.forName("UTF-8")).getBytes(gb2312);
    return led1_data;
}
public static String gbkToUtf8(byte[] gbkBytes) {
    byte[] led1_data = new String(gbkBytes, Charset.forName("GBK")).getBytes(Charset.forName("UTF-8"));
    return new String(led1_data);
}


3、modbus-crc16

/**
 * CRC-16-XMODEM校验
 * @param bytes 待校验的值
 * @return crc校验码
 */
public static int cRC16XMODEM(byte[] bytes) {
    int crc = INITIAL;
    for (byte b : bytes) {
        crc ^= (b & 0xFF) << 8; // XOR with byte value
        for (int i = 0; i < 8; i++) {
            if ((crc & 0x8000) != 0) {
                crc = (crc << 1) ^ POLYNOMIAL;
            } else {
                crc <<= 1;
            }
        }
    }
    return crc & 0xFFFF;
}

4、16字节输出日志:

呱牛笔记

public static void printHexString(String hint, byte[] b) {
    if (b == null){
        return;
    }
    StringBuilder hexBuilder = new StringBuilder();
    hexBuilder.append(hint+",b.length:"+b.length + "  ");
    for (int i = 0; i < b.length; i++) {
        String hex = Integer.toHexString(b[i] & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }
        hexBuilder.append(hex);
        hexBuilder.append(" ");
        if (i>0 && i%16==0){
            String printStr = hexBuilder.toString();
            hexBuilder = new StringBuilder();
            Log.i(TAG, printStr.toString() + " ");
        }
    };
    String printStr = hexBuilder.toString();
    Log.i(TAG, printStr.toString() + " "); 
}

5、各种数据结构,TLV:

class TLV{
    public short tag;
    public short length;
    public byte[] value;
};
int generate_tlv_msg(TLV []items,int len, byte[] msg){
    int pos = 0;
    //12字节头
    msg[pos++] = 0;
    msg[pos++] = 0;
    msg[pos++] = (byte)(TCA_GET & 0xff00 >> 8);
    msg[pos++] = (byte)(TCA_GET & 0xff);
    msg[pos++] = (byte)(seq & 0xff00 >> 8);
    msg[pos++] = (byte)(seq & 0xff);
    seq++;
    msg[pos++] = (byte)(term_id & 0xff000000 >> 24);
    msg[pos++] = (byte)(term_id & 0xff0000 >> 16);
    msg[pos++] = (byte)(term_id & 0xff00 >> 8);
    msg[pos++] = (byte)(term_id & 0xff);
    msg[pos++] = (byte)(0);
    msg[pos++] = (byte)(0);
    for (int i=0; i<len; i++){
        msg[pos++] = (byte)(items[i].tag&0xff00 >> 8);
        msg[pos++] = (byte)(items[i].tag&0x00ff);
        msg[pos++] = (byte)0;
        msg[pos++] = (byte)0;
    }
    msg[0] = (byte)(pos & 0xff00 >> 8);
    msg[1] = (byte)(pos & 0xff);
    return pos;
}






赞(2)
未经允许不得转载:工具盒子 » Java字节处理的一点经验