最近在做项目遇到一个问题,就是需要对数据进行CRC8校验,多项式是 X7+X6+X5+X2+1,对应的二进制表达是 11100101,但是因为传输反转所以我们这里的多项式二进制表达方式

为 10100111,话不多说,上代码

public class CRC8_76520 {

public static void main(String[] args) {
do_crc76520(BytesAnd16Code.hexStringToBytes("34112233445550"),7);
}

static int ENCPY = 0xE5;/* 11100101 */
/* 定义反转生成多项式*/
static int ENCPY_REV = 0xA7;/* 10100111 */
public static String do_crc76520(byte[] message, int len)
{
int uiMaxBit;
int uiRsb;
int crc_reg;

int uiMesIndex;
int uiLsb;

int i;
int crcreturn = 0x00;

uiMaxBit = len * 8 - 1;
uiRsb = 0;
crc_reg = (message[0] ^ ENCPY_REV);
while (uiRsb < uiMaxBit)
{
if ((crc_reg & 0x01) > 0x00)
{
crc_reg = crc_reg ^ ENCPY_REV;
}
else
{
crc_reg = crc_reg >> 1;
uiMesIndex = uiRsb / 8 + 1;
uiLsb = uiRsb % 8;
if (uiMesIndex < len)
{
if ((message[uiMesIndex] & (0x01 << uiLsb)) > 0x00)
{
crc_reg = crc_reg | 0x80;
}
}
uiRsb++;
}
}
//将crc从发送顺序转变为正常数据表示顺序,即最高位和最低位依次调换
for (i = 0; i < 7; i++)
{
if ((crc_reg & (0x01 << i)) > 0x00)
{
crcreturn = (crcreturn | (0x01 << (7 - i)));
}
}

System.out.println(Integer.toHexString(crcreturn));
return Integer.toHexString(crcreturn);
}

}

字节数组转16进制字符串类

class BytesAnd16Code {

public static void main(String[] args) {
float f = 120.00f;
System.out.println(Integer.toHexString(Float.floatToIntBits(f)));
}

/**
*
* @param src
* @return
*/
public static String bytesToHexString(byte[] src){
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {

int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}

/**
* Convert hex string to byte[]
* @param hexString the hex string
* @return byte[]
*/
public static byte[] hexStringToBytes(String hexString) { // 0x16
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase(); // 16
int length = hexString.length() / 2; // lenght=1
char[] hexChars = hexString.toCharArray(); //[1,6]
//1,6
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));

}
return d;
}

/**
* Convert char to byte
* @param c char
* @return byte
*/
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}

}

CRC8反转校验的更多相关文章

  1. Java获取字符串的CRC8校验码(由C程序的代码修改为了Java代码)

    CRC8算法请百度,我也不懂,这里只是把自己运行成功的结构贴出来了.方法CRC8_Tab这里没有处理,因为我的程序中没有用到. package com.crc; public class CCRC8_ ...

  2. CRC8校验

    static u8 crccheck(u8* p,u8 len) //CRC校验,返回CRC检验值 { u8 bit0,cbit,i,j,byte,temp; temp = ; ; j < le ...

  3. CRC校验源码分析

    这两天做项目,需要用到 CRC 校验.以前没搞过这东东,以为挺简单的.结果看看别人提供的汇编源程序,居然看不懂.花了两天时间研究了一下 CRC 校验,希望我写的这点东西能够帮助和我有同样困惑的朋友节省 ...

  4. CRC校验8

    什么是CRC校验? CRC即循环冗余校验码:是数据通信领域中最常用的一种查错校验码,其特征是信息字段和校验字段的长度可以任意选定.循环冗余检查(CRC)是一种数据传输检错功能,对数据进行多项式计算,并 ...

  5. 用C#实现的几种常用数据校验方法整理(CRC校验;LRC校验;BCC校验;累加和校验)

    CRC即循环冗余校验码(Cyclic Redundancy Check):是数据通信领域中最常用的一种查错校验码,其特征是信息字段和校验字段的长度可以任意选定.循环冗余检查(CRC)是一种数据传输检错 ...

  6. CEIWEI CheckSum CRC校验精灵v2.1 CRC3/CRC4/CRC5/CRC6/CRC8CRC10/CRC11/CRC16/CRC24/CRC32/CRC40/CRC64/CRC82/Adler32

    CEIWEI CheckSum CRC校验精灵 是一款通用的循环冗余校验码CRC(Cyclic Redundancy Check).MD5.SHA1.SHA2.SHA3.HAVAL.SHAKE.TIG ...

  7. CRC校验算法详解

    CRC(Cyclic Redundancy Check)循环冗余校验是常用的数据校验方法,讲CRC算法的文章很多,之所以还要写这篇,是想换一个方法介绍CRC算法,希望能让大家更容易理解CRC算法. 先 ...

  8. 最详细易懂的CRC-16校验原理(附源程序)(转)

    最详细易懂的CRC-16校验原理(附源程序) from:http://www.openhw.org/chudonganjin/blog/12-08/230184_515e6.html 最详细易懂的CR ...

  9. 校验码(海明校验,CRC冗余校验,奇偶校验)

    循环冗余校验码 CRC码利用生成多项式为k个数据位产生r个校验位进行编码,其编码长度为n=k+r所以又称 (n,k)码. CRC码广泛应用于数据通信领域和磁介质存储系统中. CRC理论非常复杂,一般书 ...

随机推荐

  1. react state成员

    组件中包括state,props与render成员函数. react中,主要通过定义state,根据不同state渲染对应用户界面. 过程调用了成员函数setState(data,callback). ...

  2. Disable access to Windows Update

    Disable access to Windows Update If this policy setting is enabled, all Windows Update features are ...

  3. 文献导读 | Single-Cell Sequencing of iPSC-Dopamine Neurons Reconstructs Disease Progression and Identifies HDAC4 as a Regulator of Parkinson Cell Phenotypes

    文献编号:19Mar - 11 2019年04月23日三读,会其精髓: 相信这种方法的话,那么它的精髓是什么,如何整合出这个core gene set. 首先要考虑样本的选择,样本里是否存在明显的分层 ...

  4. MIUI8系统完整刷入开发版开启root权限的经验

    小米的机器不同手机型号一般情况官网都提供两个不同的安卓系统版本,可分为稳定版和开发版,稳定版没有提供root超级权限管理,开发版中就支持了root超级权限,很多情况我们需要使用的一些功能强大的APP, ...

  5. 8.5 GOF设计模式四: 观察者模式Observer

    GOF设计模式四: 观察者模式Observer  现实中遇到的问题  当有许多不同的客户都对同一数据源感兴趣,对相同的数据有不同的处理方式,该如 何解决?5.1 定义: 观察者模式  观察者模式 ...

  6. sqlserver 查询表锁死,解除表锁死

    查询锁死的表名以及ID select request_session_id id, OBJECT_NAME(resource_associated_entity_id) tableName FROM ...

  7. SVN上传文件自动更新到发布站点

    源码安装svn, version 1.9.5 ###########服务端源码安装############# IP:192.168.1.13 安装依赖:              # yum -y i ...

  8. smb 访问时 提示权限不够

    1. 确认 防火墙关闭和getenforce 为Permissive 状态. 关闭防火墙 service iptables stop 关闭  setenforce 0 2.windows 登录切换 身 ...

  9. GNOME 系统设置

    详细的GNOME系统设置全文,参见这里. 以下摘录使用到的部分. 1. 在任务栏上显示日期或周几(二选一).秒数 $ gsettings set org.gnome.desktop.interface ...

  10. vue 的全局组件和 局部组件

    vue组件局部与全局注册的区别   //局部注册 var mycomponent = new extend({        <!--Vue.extend()是Vue构造器的扩展,调用Vue.e ...