请问五个字节的十六进制有什么好方法计算行列奇偶位

2020-01-19 19:38发布

最近在研究ID卡读写请问有什么好方法能实现将 五个字节的十六进制每半个字节计算出其对应的奇偶位,然后将每半个字节加奇偶位共 5BIT排列成十行再对每列奇偶位计算,
最后在得到的55BIT前面加上 9个1 共得到 64BIT再每8BIT为一个字节分成8个字节的十六进制数呢?谢谢!
例子:
              //如下为 ID = 3000F94989 转码过程
                      111111111 --->九个头
                                       0011        0              3
                                       0000        0              0
                                   ---------------------------------------  
                                       0000        0              0  
                                       0000        0              0   
                                ------------------------------------------
                                       1111        0              F
                                       1001        0              9
                          --------------------------------------------------
                                       0100        1              4
                                       1001        0              9
                             -------------------------------------------------
                                       1000        1              8
                                       1001        0              9  
                           ---------------------------------------------      
                                       1001        0      --->列奇偶位
转换结果为:0XFF / 0X98 / 0X00 / 0X07 / 0XA4 / 0X99 / 0X46 / 0X52
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
6条回答
zouzhichao
2020-01-20 03:36
  1. #include <stdio.h>

  2. typedef unsigned char uint8_t;

  3. uint8_t col_check(const uint8_t* buffer)
  4. {
  5.         uint8_t check;
  6.         uint8_t temp;
  7.         check = *buffer++;
  8.         check ^= *buffer++;
  9.         check ^= *buffer++;
  10.         check ^= *buffer++;
  11.         check ^= *buffer;
  12.         check ^= check >> 4;
  13.         temp = check ^ (check >> 2);
  14.         temp ^= temp >> 1;
  15.         return ((check & 0x0f) << 1) | (temp & 0x01);
  16. }

  17. void code_create(const uint8_t* buffer, uint8_t* code)
  18. {
  19.         uint8_t check_col;
  20.         uint8_t check_row;
  21.         uint8_t temp;
  22.         check_col = col_check(buffer);
  23.         *code++ = 0xff;
  24.        
  25.         temp = *buffer++;
  26.         check_row = temp ^ (temp >> 2);
  27.         check_row ^= (check_row >> 1);
  28.         *code = 0x80; // fill 1
  29.         *code |= temp >> 1; // fill 4
  30.         *code &= 0xf8; // all 5
  31.         *code |= (check_row & 0x10) >> 2; // fill 1
  32.         *code++ |= (temp >> 2) & 0x03; // fill 2
  33.        
  34.         *code = temp << 6; // fill 2
  35.         *code |= (check_row & 0x01) << 5; // fill 1
  36.         temp = *buffer++;
  37.         check_row = temp ^ (temp >> 2);
  38.         check_row ^= (check_row >> 1);
  39.         *code |= temp >> 3; // fill 4
  40.         *code &= 0xfe; // all 7
  41.         *code++ |= (check_row & 0x10) >> 4; // fill 8
  42.        
  43.         *code = temp << 4; // fill 4
  44.         *code |= (check_row & 0x01) << 3; // fill 1
  45.         temp = *buffer++;
  46.         check_row = temp ^ (temp >> 2);
  47.         check_row ^= (check_row >> 1);
  48.         *code++ |= temp >> 5; // fill 3
  49.        
  50.         *code = temp << 3; // fill 1
  51.         *code &= 0x80; // all 1
  52.         *code |= (check_row & 0x10) << 2; // fill 1
  53.         *code |= (temp & 0x0f) << 2; // fill 4
  54.         *code &= 0xfc; // all 6
  55.         *code |= (check_row & 0x01) << 1; // fill 1
  56.         temp = *buffer++;
  57.         check_row = temp ^ (temp >> 2);
  58.         check_row ^= (check_row >> 1);
  59.         *code++ |= temp >> 7; // fill 1
  60.        
  61.         *code = temp << 1; // fill 3
  62.         *code &= 0xe0; // all 3
  63.         *code |= check_row & 0x10; // fill 1
  64.         *code++ |= temp & 0x0f; // fill 4
  65.        
  66.         *code = (check_row & 0x01) << 7; // fill 1
  67.         temp = *buffer++;
  68.         check_row = temp ^ (temp >> 2);
  69.         check_row ^= (check_row >> 1);
  70.         *code |= temp >> 1; // fill 4
  71.         *code &= 0xf8; // all 5
  72.         *code |= (check_row & 0x10) >> 2; // fill 1
  73.         *code++ |= (temp >> 2) & 0x03; // fill 2
  74.        
  75.         *code = temp << 6; // fill 2
  76.         *code |= (check_row & 0x01) << 5; // fill 1
  77.         *code |= check_col;
  78. }

  79. int main(int argc, char *argv[])
  80. {
  81.         uint8_t buffer[5] = {0x30, 0x00, 0xf9, 0x49, 0x89};
  82.         uint8_t res[8];
  83.         code_create((const uint8_t*)buffer, (uint8_t*)res);
  84.         printf("0x%02x ", res[0]);
  85.         printf("0x%02x ", res[1]);
  86.         printf("0x%02x ", res[2]);
  87.         printf("0x%02x ", res[3]);
  88.         printf("0x%02x ", res[4]);
  89.         printf("0x%02x ", res[5]);
  90.         printf("0x%02x ", res[6]);
  91.         printf("0x%02x ", res[7]);
  92.         return 0;
  93. }
复制代码

Cfree+GCC测试通过!

QQ截图20151001020753.jpg (16.72 KB, 下载次数: 0)

下载附件

2015-10-1 02:05 上传

一周热门 更多>