byte间的异或处理 ----- 在线等

xxy802 2016-08-11 11:40:18
strSend 是穿进去的字符串,这个串很长,下面只写出来一部分,为什么有的时候算的结果是对的,有的时候是错的呢
在线等高手看看,谢谢
/ **
* 异域校验帧结果
*/
public void XorResult(String strSend){
strSend = "1A 14 04 03 15 016 0C 12 00 0F 0E 0D 0C 0B 0A 09 08 0A 02 05 16 12 1A 1F 15";

String strValue[] = strSend.split(" ");

byte strBat[] =new byte[strValue.length] ;

for(int i=0;i<strValue.length;i++)
{
strBat[i] = (byte) Long.parseLong(strValue[i],16);
}


// byte a[] = strSend.getBytes();
int ChkSum = 0;
for (int i = 0; i < strBat.length ; i++) {
if(i>0){
if(i == 1){
ChkSum = (int) (strBat[i]);
}else{
ChkSum = (int) (ChkSum ^ strBat[i]);
}
}

}

String ssXorResult = Integer.toHexString(ChkSum);


// XorResult = ssXorResult.substring(ssXorResult.length()-3,ssXorResult.length());
XorResult = ssXorResult.replaceAll("[a-zA-Z]","");

if(XorResult.length() == 1){
XorResult = "0"+XorResult;
}
XorResult = XorResult+" ";
}
...全文
294 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xxy802 2016-08-11
  • 打赏
  • 举报
回复
// XorResult = ssXorResult.substring(ssXorResult.length()-3,ssXorResult.length()); XorResult = ssXorResult.replaceAll("[a-zA-Z]",""); 这两句话是多余的处理,因为有的时候算出来的结果竟然是 ffffe8 这样的,肯定不对
xxy802 2016-08-11
  • 打赏
  • 举报
回复
XorResult = ssXorResult.substring(ssXorResult.length()-3,ssXorResult.length()); 改成 XorResult = ssXorResult.substring(ssXorResult.length()-2,ssXorResult.length()); 就可以了 这个算法没问题
xxy802 2016-08-11
  • 打赏
  • 举报
回复
自己顶下自己顶下

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧