package cn.hylexus.jt808.util;
import java.util.Arrays;
import java.util.List;
public class BitOperator {
/**
* 把一个整形该为byte
*
* @param value
* @return
* @throws Exception
*/
public byte integerTo1Byte(int value) {
return (byte) (value & 0xFF);
}
/**
* 把一个整形该为1位的byte数组
*
* @param value
* @return
* @throws Exception
*/
public byte[] integerTo1Bytes(int value) {
byte[] result = new byte[1];
result[0] = (byte) (value & 0xFF);
return result;
}
/**
* 把一个整形改为2位的byte数组
*
* @param value
* @return
* @throws Exception
*/
public byte[] integerTo2Bytes(int value) {
byte[] result = new byte[2];
result[0] = (byte) ((value >>> 8) & 0xFF);
result[1] = (byte) (value & 0xFF);
return result;
}
/**
* 把一个整形改为3位的byte数组
*
* @param value
* @return
* @throws Exception
*/
public byte[] integerTo3Bytes(int value) {
byte[] result = new byte[3];
result[0] = (byte) ((value >>> 16) & 0xFF);
result[1] = (byte) ((value >>> 8) & 0xFF);
result[2] = (byte) (value & 0xFF);
return result;
}
/**
* 把一个整形改为4位的byte数组
*
* @param value
* @return
* @throws Exception
*/
public byte[] integerTo4Bytes(int value){
byte[] result = new byte[4];
result[0] = (byte) ((value >>> 24) & 0xFF);
result[1] = (byte) ((value >>> 16) & 0xFF);
result[2] = (byte) ((value >>> 8) & 0xFF);
result[3] = (byte) (value & 0xFF);
return result;
}
/**
* 把byte[]转化位整形,通常为指令用
*
* @param value
* @return
* @throws Exception
*/
public int byteToInteger(byte[] value) {
int result;
if (value.length == 1) {
result = oneByteToInteger(value[0]);
} else if (value.length == 2) {
result = twoBytesToInteger(value);
} else if (value.length == 3) {
result = threeBytesToInteger(value);
} else if (value.length == 4) {
result = fourBytesToInteger(value);
} else {
result = fourBytesToInteger(value);
}
return result;
}
/**
* 把一个byte转化位整形,通常为指令用 0xFF=255
*
* @param value
* @return
* @throws Exception
*/
public int oneByteToInteger(byte value) {
return (int) value & 0xFF;
}
public static void main(String[] args) {
BitOperator b = new BitOperator();
int i = b.oneByteToInteger((byte) 49);
System.out.println(i);
System.out.println(255 == 0xff);
System.out.println(5 | 6);
}
/**
* 把一个2位的数组转化位整形
*
* @param value
* @return
* @throws Exception
*/
public int twoBytesToInteger(byte[] value) {
// if (value.length < 2) {
// throw new Exception("Byte array too short!");
// }
int temp0 = value[0] & 0xFF;
int temp1 = value[1] & 0xFF;
return ((temp0 << 8) + temp1);
}
/**
* 把一个3位的数组转化位整形
*
* @param value
* @return
* @throws Exception
*/
public int threeBytesToInteger(byte[] value) {
int temp0 = value[0] & 0xFF;
int temp1 = value[1] & 0xFF;
int temp2 = value[2] & 0xFF;
return ((temp0 << 16) + (temp1 << 8) + temp2);
}
/**
* 把一个4位的数组转化位整形,通常为指令用
*
* @param value
* @return
* @throws Exception
*/
public int fourBytesToInteger(byte[] value) {
// if (value.length < 4) {
// throw new Exception("Byte array too short!");
// }
int temp0 = value[0] & 0xFF;
int temp1 = value[1] & 0xFF;
int temp2 = value[2] & 0xFF;
int temp3 = value[3] & 0xFF;
return ((temp0 << 24) + (temp1 << 16) + (temp2 << 8) + temp3);
}
/**
* 把一个4位的数组转化位整形
*
* @param value
* @return
* @throws Exception
*/
public long fourBytesToLong(byte[] value) throws Exception {
// if (value.length < 4) {
// throw new Exception("Byte array too short!");
// }
int temp0 = value[0] & 0xFF;
int temp1 = value[1] & 0xFF;
int temp2 = value[2] & 0xFF;
int temp3 = value[3] & 0xFF;
return (((long) temp0 << 24) + (temp1 << 16) + (temp2 << 8) + temp3);
}
/**
* 把一个数组转化长整形
*
* @param value
* @return
* @throws Exception
*/
public long bytes2Long(byte[] value) {
long result = 0;
int len = value.length;
int temp;
for (int i = 0; i < len; i++) {
temp = (len - 1 - i) * 8;
if (temp == 0) {
result += (value[i] & 0x0ff);
} else {
result += (value[i] & 0x0ff) << temp;
}
}
return result;
}
/**
* 把一个长整形改为byte数组
*
* @param value
* @return
* @throws Exception
*/
public byte[] longToBytes(long value){
return longToBytes(value, 8);
}
/**
* 把一个长整形改为byte数组
*
* @param value
* @return
* @throws Exception
*/
public byte[] longToBytes(long value, int len) {
byte[] result = new byte[len];
int temp;
for (int i = 0; i < len; i++) {
temp = (len - 1 - i) * 8;
if (temp == 0) {
result[i] += (value & 0x0ff);
} else {
result[i] += (value >>> temp) & 0x0ff;
}
}
return result;
}
/**
* 得到一个消息ID
*
* @return
* @throws Exception
*/
public byte[] generateTransactionID() throws Exception {
byte[] id = new byte[16];
System.arraycopy(integerTo2Bytes((int) (Math.random() * 65536)), 0, id, 0, 2);
System.arraycopy(integerTo2Bytes((int) (Math.random() * 65536)), 0, id, 2, 2);
System.arraycopy(integerTo2Bytes((int) (Math.random() * 65536)), 0, id, 4, 2);
System.arraycopy(integerTo2Bytes((int) (Math.random() * 65536)), 0, id, 6, 2);
System.arraycopy(integerTo2Bytes((int) (Math.random() * 65536)), 0, id, 8, 2);
System.arraycopy(integerTo2Bytes((int) (Math.random() * 65536)), 0, id, 10, 2);
System.arraycopy(integerTo2Bytes((int) (Math.random() * 65536)), 0, id, 12, 2);
System.arraycopy(integerTo2Bytes((int) (Math.random() * 65536)), 0, id, 14, 2);
return id;
}
/**
* 把IP拆分位int数组
*
* @param ip
* @return
* @throws Exception
*/
public int[] getIntIPValue(String ip) throws Exception {
String[] sip = ip.split("[.]");
// if (sip.length != 4) {
// throw new Exception("error IPAddress");
// }
int[] intIP = { Integer.parseInt(sip[0]), Integer.parseInt(sip[1]), Integer.parseInt(sip[2]),
Integer.parseInt(sip[3]) };
return intIP;
}
/**
* 把byte类型IP地址转化位字符串
*
* @param address
* @return
* @throws Exception
*/
public String getStringIPValue(byte[] address) throws Exception {
int first = this.oneByteToInteger(address[0]);
int second = this.oneByteToInteger(address[1]);
int third = this.oneByteToInteger(address[2]);
int fourth = this.oneByteToInteger(address[3]);
return first + "." + second + "." + third + "." + fourth;
}
/**
* 合并字节数组
*
* @param first
* @param rest
* @return
*/
public byte[] concatAll(byte[] first, byte[]... rest) {
int totalLength = first.length;
for (byte[] array : rest) {
if (array != null) {
totalLength += array.length;
}
}
byte[] result = Arrays.copyOf(first, totalLength);
int offset = first.length;
for (byte[] array : rest) {
if (array != null) {
System.arraycopy(array, 0, result, offset, array.length);
offset += array.length;
}
}
return result;
}
/**
* 合并字节数组
*
* @param rest
* @return
*/
public byte[] concatAll(List<byte[]> rest) {
int totalLength = 0;
for (byte[] array : rest) {
if (array != null) {
totalLength += array.length;
}
}
byte[] result = new byte[totalLength];
int offset = 0;
for (byte[] array : rest) {
if (array != null) {
System.arraycopy(array, 0, result, offset, array.length);
offset += array.length;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Java Netty 版 Jt808协议解析工程
共88个文件
java:23个
master:4个
prefs:3个
3星 · 超过75%的资源 需积分: 39 255 下载量 164 浏览量
2017-04-10
22:39:36
上传
评论 12
收藏 94KB ZIP 举报
温馨提示
Java Netty版完全符合JT808部标文档的开发规范。直接可以下载使用,不信拉倒。
资源推荐
资源详情
资源评论
收起资源包目录
jt808-tcp-netty.zip (88个子文件)
jt808-tcp-netty
.project 567B
src
main
test
MainTest.java 104B
resources
log4j.xml 6KB
log4j.dtd 7KB
java
cn
hylexus
jt808
common
TPMSConsts.java 1KB
MyClassificationLogAppender.java 330B
service
codec
Decoder4LoggingOnly.java 1KB
MsgDecoder.java 8KB
MsgEncoder.java 4KB
BaseMsgProcessService.java 1KB
TerminalMsgProcessService.java 4KB
handler
TCPServerHandler.java 7KB
util
BCD8421Operater.java 2KB
ts
ConvertValue.java 487B
PrintHex.java 544B
HexStringUtils.java 2KB
JT808ProtocolUtils.java 4KB
BitOperator.java 10KB
vo
PackageData.java 5KB
resp
ServerCommonRespMsgBody.java 1KB
TerminalRegisterMsgRespBody.java 1KB
req
TerminalRegisterMsg.java 4KB
TerminalAuthenticationMsg.java 1KB
Session.java 3KB
server
TCPServer.java 4KB
SessionManager.java 3KB
target
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.jdt.core.prefs 243B
org.eclipse.core.resources.prefs 124B
.git
logs
HEAD 163B
refs
heads
master 163B
remotes
origin
master 140B
index 3KB
COMMIT_EDITMSG 18B
objects
a6
f85d42d6e0ec8c6a335ddc291fa4af4ca41ff2 1KB
pack
25
f4f84309db7c651d1398a4074cdb3c1b4d52ff 160B
16
5754ee9a03caee29c4126c0f5de594af4dca11 614B
info
4c
0613c5f1d8160e8748a581eb7478b1983b08c9 46B
f8
e433a50e6adefddf77c94b306812112942cf0f 2KB
9c
2d5baa449b33f45b50a90a7f195c3f4a2b4269 234B
de
76c40e4b3dc5ef3244455788b106732e235fb3 224B
8fd6529457289b7b5f8d50e8de819ad76613d3 107B
d8
ccba91722b27060921bc746f5cc1601e27a10b 107B
14
37c39aacaacf2ddd52cf6e494c0dffbcff88ea 2KB
40
96eb2f4363ec46d437ea3a4289e5ca8ec380c5 81B
48
c2b4811f175519b61c76cb7c134c2e7c396fa9 1KB
88
19e5be68e9ae884164650704962c3385b8bc0d 109B
e0
7f091d5605f1b9e04c0bef0f90768888f515ef 62B
97
268e4dc5aa2d81c06bc1bace001f66ee837c5f 119B
81
6e83aa572a3dab1934405a6d36aec27e8ffc09 2KB
53
30741711847f8cf833e174d704441752a893eb 661B
5b
599e875fcd849423428db68ab4b76972844f53 116B
cc
26c20a882b5e568c80148f4ce7bc228a6e4798 49B
f5
36ef9c6f39c1e5639d01bb31c26efdc61578af 1KB
8e
e015aafe04d0ae95a9d99bbec5a6c98e6c3cab 988B
3b
ff9d4791ce4fa2f18ee479bdbd274ea6890295 96B
31
8009388e46dc6b0f80e621a97c050a68fa46a8 112B
f2
b0dcd177e3d85ac8d8240c84d971d688d52779 65B
c3
2db0d84f42c29bbedde5f9028bff15a16c5a89 1KB
fe
e0ae938791dd9bf08b6217233f74e30efeeb7d 1KB
7d
a11948439c9c3e803fbf489d52861b94ec96b8 365B
c1
27f4754f926a5eb37139fde88483af89b539d8 44B
20
93f26fac6ad7b92e7de46e909fc664bdcd2aca 753B
4f
4a0da80f6b727083c19bca7ebaa46d4a69e31b 47B
28
5f0986b59fa2b4973dedcc39dfafb3b1772275 607B
cf
0b5dc6c126bfb6bda70f26863c68b29cd7a731 904B
7c
93732f8aa4083998e3882d44ccfd67d547ea1a 794B
9e
77cfe667431bf0956e74c93eaaae79261a198d 169B
92
76d6d553abb2a8d051d728f2c44f9546a03bb4 270B
38
b33c459acbb926359092d2c98c6ee9b0d157a7 143B
01
bc5d4e977a859b568c6cd37280e8f4d7197030 149B
3e
1a33c72eaced26259e61a17f4cb6fddb84b068 1KB
06
401e1d0a6e40e929251832666af562080b8e3c 616B
ea
37fc323078cf09e62e986631b3d342de0e2818 459B
7f
4436b8863bc0a1f14b6a4b493a7d9d4c854cc1 127B
ec
fe80c3dd677df15d16f0f8e2937f537f98cf7d 1KB
1c
e3a44d4189a0040f8bf56bea6b1ddb76ead7b5 963B
57
aaebfff47c1d7d24e278002fd6bc2fe7a2515d 58B
e7
f2bf67d1653c0a1277646b5f41124f8d49e3bb 856B
d6
0570f2e9de810db391b0d36b4cc6f79b1e31ea 2KB
51
31c5c0dcedeeb02aa1a81cccad3137b41b750a 584B
HEAD 23B
config 253B
refs
heads
master 41B
tags
remotes
origin
master 41B
branches
hooks
pom.xml 3KB
.classpath 1KB
.gitignore 2KB
共 88 条
- 1
灵嗅
- 粉丝: 203
- 资源: 39
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页