简体   繁体   English

Java - 如何计算BitSet的CRC16

[英]Java - How to calculate CRC16 of BitSet

I Have a Java BitSet where i have some data.我有一个 Java BitSet,其中有一些数据。 The length of this BitSet is 545 bits.这个 BitSet 的长度是 545 位。 Problem: All current known implementations can only work with a byte array, but converting my BitSet to a byte array will change the data because i need to do some padding.问题:所有当前已知的实现只能使用字节数组,但是将我的 BitSet 转换为字节数组会更改数据,因为我需要进行一些填充。 Is there any known implementation which can handly my data without needing to adjust it to whole bytes?是否有任何已知的实现可以处理我的数据而无需将其调整为整个字节?

Use .toByteArray() to get the bits into a sequence of bytes.使用.toByteArray()将位转换为字节序列。 You need to know what CRC-16 definition is required (polynomial, ordering, pre and post processing), and the order in which to process the bits.您需要知道需要什么 CRC-16 定义(多项式、排序、预处理和后处理),以及处理位的顺序。 .toByteArray() will put the first bit in the set in the least-significant bit of the first byte. .toByteArray()会将集合中的第一位放在第一个字节的最低有效位中。

Then you can use crcany to generate C code for the CRC-16 you need.然后就可以使用crcany为你需要的CRC-16生成C码了。 The generated code includes a crc16..._rem() routine for updating the CRC with a number of bits.生成的代码包括一个crc16..._rem()例程,用于用多个位更新 CRC。 For a BitSet with n bits, you would first compute the CRC on the first n >> 3 bytes.对于具有n位的 BitSet,您将首先计算前n >> 3个字节的 CRC。 Then use crc16..._rem() to update the CRC using the n & 7 bits in the last byte.然后使用crc16..._rem()使用最后一个字节中的n & 7位更新 CRC。 It is straightforward to convert the C code to Java.将 C 代码转换为 Java 很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM