您的位置:首页 > 其它

计算8583报文长度,两个字节表示

2012-05-08 10:07 387 查看
方法1
/**
* 计算报文长度,两字节报文长度(高位在前)
*
* @param len
* @return
*/
public byte[] getByteMessageLen(int len) {
byte[] buf = new byte[2];
//取高8位
buf[0] = (byte) (len >> 8);
//取低8
buf[1] = (byte) (len & 0xff);
return buf;
}
方法2
// 获取第一个高位长度
byte first = (byte) (len / 100);
// 获取第二个高位长度
byte second = (byte) (((len % 100) / 10) << 4 | (len % 10));


//方法1
/**
*计算8583表针报文前两字节长度,
*如:数据 0, 115, 0, 0, 0, 0, 0, 96, 0, 0, 0, 0........
*前面的0,115表示报文的长  度,获取报文长度, 两字节16进制报文长度(高位在前),,
*例如300字节长报文,长度填0x012C ,变为 blen[1] & 0xff 编程无符号整形
*/
public static int getMessageLen(byte[] blen) {
String hexlen = Integer.toHexString(blen[0]) + Integer.toHexString(blen[1] & 0xff);
return Integer.parseInt(hexlen, 16);
}
方法2
int length = ((buf[pos] & 0x0f) * 100) + (((buf[pos + 1] & 0xf0) >> 4) * 10)+ (buf[pos + 1] & 0x0f);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐