Android的CRC-CCITT [英] Android CRC-CCITT

查看:296
本文介绍了Android的CRC-CCITT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个CRC校验申请我写的,但不能想出了在网上code和计算器什么即时做错了。我可能只是不明白它正确。这正是我需要的:

I need a CRC check for an application Im writing, but cant figure out for the online code and calculators what im doing wrong. I may just not understand it correctly. This is what I need :

它采用CRC-CCITT与0xFFFF与反向输入位顺序初始值。例如,获取设备类型的信息是:0×01,0×06,0×01,0×00,0x0B中,0xD9。 CRC是0xD90B。

这是使用code林:

public static int CRC16CCITT(byte[] bytes) {
    int crc = 0xFFFF;          // initial value
    int polynomial = 0x1021;   // 0001 0000 0010 0001  (0, 5, 12) 

    for (byte b : bytes) {
        for (int i = 0; i < 8; i++) {
            boolean bit = ((b   >> (7-i) & 1) == 1);
            boolean c15 = ((crc >> 15    & 1) == 1);
            crc <<= 1;
            if (c15 ^ bit) crc ^= polynomial;
         }
    }

    crc &= 0xffff;
    //System.out.println("CRC16-CCITT = " + Integer.toHexString(crc));
    return crc;

我写它的Andr​​oid设备,以便需要在Java中。

Im writing it for an Android device so needs to be in java.

推荐答案

下面是我在我的应用程序(它的工作原理)。

Here is the code that I use in my App (It works).

static public int GenerateChecksumCRC16(int bytes[]) {

        int crc = 0xFFFF;
        int temp;
        int crc_byte;

        for (int byte_index = 0; byte_index < bytes.length; byte_index++) {

            crc_byte = bytes[byte_index];

            for (int bit_index = 0; bit_index < 8; bit_index++) {

                temp = ((crc >> 15)) ^ ((crc_byte >> 7));

                crc <<= 1;
                crc &= 0xFFFF;

                if (temp > 0) {
                    crc ^= 0x1021;
                    crc &= 0xFFFF;
                }

                crc_byte <<=1;
                crc_byte &= 0xFF;

            }
        }

        return crc;
    }

这篇关于Android的CRC-CCITT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆