| | |
| | | |
| | | import android.util.Log; |
| | | |
| | | import com.anyun.exam.lib.MyLog; |
| | | import com.anyun.im_lib.util.ByteUtil; |
| | | |
| | | import safeluck.drive.evaluation.util.Utils; |
| | | |
| | | /** |
| | | * MyApplication2 |
| | |
| | | pos+=messageBodyBytes.length; |
| | | |
| | | //校验码 |
| | | // TODO: 2019/12/18 校验码需要计算 还有转义需要处理 |
| | | checkCode = Utils.calCheckCode(ByteUtil.subArray(desBytes,1,pos-1)); |
| | | desBytes[pos] = checkCode; |
| | | pos++; |
| | | //末尾结束标识位 |
| | | desBytes[pos] = MESSAGE_TAIL; |
| | | |
| | | Log.i(TAG, "包长度="+(pos+1)); |
| | | Log.i(TAG, "包内容: "+ByteUtil.byte2HexStr(desBytes)); |
| | | |
| | | return desBytes; |
| | | MyLog.i(TAG, "原始包长度="+(pos+1)); |
| | | MyLog.i(TAG, "原始包内容: "+ByteUtil.byte2HexStr(desBytes)); |
| | | byte[] tranferbytes = Utils.transferMeaning(desBytes); |
| | | MyLog.i(TAG,"转义后的包内容:"+ByteUtil.byte2HexStr(tranferbytes)); |
| | | return tranferbytes; |
| | | } |
| | | |
| | | } |
| | |
| | | package safeluck.drive.evaluation.util; |
| | | |
| | | import android.content.res.Resources; |
| | | import android.util.Log; |
| | | import android.util.TypedValue; |
| | | |
| | | import com.anyun.exam.lib.MyLog; |
| | | import com.anyun.exam.lib.util.ByteUtil; |
| | | |
| | | /** |
| | | * MyApplication2 |
| | |
| | | * All Rights Saved! Chongqing AnYun Tech co. LTD |
| | | */ |
| | | public class Utils { |
| | | private static final String TAG = "Utils"; |
| | | public static float px2dp(float value){ |
| | | return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,value, Resources.getSystem().getDisplayMetrics()); |
| | | } |
| | |
| | | public static float dp2Px(int dpValue){ |
| | | return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,dpValue,Resources.getSystem().getDisplayMetrics()); |
| | | } |
| | | |
| | | /** |
| | | * 平台协议生成的校验码 |
| | | * @param bytes |
| | | * @return |
| | | */ |
| | | |
| | | public static byte calCheckCode(byte[] bytes){ |
| | | byte checkCode =0; |
| | | // 参与运算的两个值,如果两个相应bit位相同,则结果为0,否则为1。 |
| | | for (int i = 0; i < bytes.length; i++) { |
| | | checkCode ^= bytes[i]; |
| | | } |
| | | |
| | | return checkCode; |
| | | } |
| | | |
| | | /** |
| | | * 转义 |
| | | * 采用Ox7e表示,若校验码、消息头以及消息体中出现0x7e,则要进行转义处理,转义规则定义如下: |
| | | * 0x7e<——>0x7d后紧跟一个0x02; |
| | | * 0x7d<——>0x7d后紧跟一个0x01。 |
| | | * @param datas |
| | | * @return |
| | | */ |
| | | public static byte[] transferMeaning(byte[] datas){ |
| | | byte [] temp = new byte[datas.length*2]; |
| | | int y = 0; |
| | | temp[y++] = 0x7e; |
| | | for (int i = 1; i < datas.length-1; i++) { |
| | | if (datas[i] == 0x7E) { |
| | | temp[y++] = 0x7D; |
| | | temp[y++] = 0x02; |
| | | } else if (datas[i] == 0x7D) { |
| | | temp[y++] = 0x7D; |
| | | temp[y++] = 0x01; |
| | | } else { |
| | | temp[y++] = datas[i]; |
| | | } |
| | | } |
| | | temp[y++] = 0x7e; |
| | | Log.i(TAG,"转义过后:"+ ByteUtil.byte2hex(temp)); |
| | | return temp; |
| | | } |
| | | } |
| | |
| | | package com.anyun.im_lib.util; |
| | | |
| | | import android.text.TextUtils; |
| | | import android.util.Log; |
| | | |
| | | import java.nio.ByteBuffer; |
| | | import java.nio.CharBuffer; |
| | |
| | | */ |
| | | public class ByteUtil { |
| | | |
| | | |
| | | private static final String TAG = "ByteUtil"; |
| | | /** |
| | | * @功能: BCD码转为10进制串(阿拉伯数据) |
| | | * @参数: BCD码 |
| | |
| | | byte b = (byte) a; |
| | | bbt[p] = b; |
| | | } |
| | | Log.i(TAG, "str2Bcd: "+byte2HexStr(bbt)); |
| | | return bbt; |
| | | } |
| | | |
| | |
| | | System.out.println(byte2HexStr(shortGetBytes((short) b))); |
| | | } |
| | | |
| | | public static byte[] subArray(byte[] srcBytes, int begin, int length) { |
| | | byte[] bytes = new byte[length]; |
| | | System.arraycopy(srcBytes,begin,bytes,0,length); |
| | | Log.i(TAG, "subArray: "+byte2HexStr(bytes)); |
| | | return bytes; |
| | | } |
| | | } |