最近在研究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
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
- #include <stdio.h>
- typedef unsigned char uint8_t;
- uint8_t col_check(const uint8_t* buffer)
- {
- uint8_t check;
- uint8_t temp;
- check = *buffer++;
- check ^= *buffer++;
- check ^= *buffer++;
- check ^= *buffer++;
- check ^= *buffer;
- check ^= check >> 4;
- temp = check ^ (check >> 2);
- temp ^= temp >> 1;
- return ((check & 0x0f) << 1) | (temp & 0x01);
- }
- void code_create(const uint8_t* buffer, uint8_t* code)
- {
- uint8_t check_col;
- uint8_t check_row;
- uint8_t temp;
- check_col = col_check(buffer);
- *code++ = 0xff;
-
- temp = *buffer++;
- check_row = temp ^ (temp >> 2);
- check_row ^= (check_row >> 1);
- *code = 0x80; // fill 1
- *code |= temp >> 1; // fill 4
- *code &= 0xf8; // all 5
- *code |= (check_row & 0x10) >> 2; // fill 1
- *code++ |= (temp >> 2) & 0x03; // fill 2
-
- *code = temp << 6; // fill 2
- *code |= (check_row & 0x01) << 5; // fill 1
- temp = *buffer++;
- check_row = temp ^ (temp >> 2);
- check_row ^= (check_row >> 1);
- *code |= temp >> 3; // fill 4
- *code &= 0xfe; // all 7
- *code++ |= (check_row & 0x10) >> 4; // fill 8
-
- *code = temp << 4; // fill 4
- *code |= (check_row & 0x01) << 3; // fill 1
- temp = *buffer++;
- check_row = temp ^ (temp >> 2);
- check_row ^= (check_row >> 1);
- *code++ |= temp >> 5; // fill 3
-
- *code = temp << 3; // fill 1
- *code &= 0x80; // all 1
- *code |= (check_row & 0x10) << 2; // fill 1
- *code |= (temp & 0x0f) << 2; // fill 4
- *code &= 0xfc; // all 6
- *code |= (check_row & 0x01) << 1; // fill 1
- temp = *buffer++;
- check_row = temp ^ (temp >> 2);
- check_row ^= (check_row >> 1);
- *code++ |= temp >> 7; // fill 1
-
- *code = temp << 1; // fill 3
- *code &= 0xe0; // all 3
- *code |= check_row & 0x10; // fill 1
- *code++ |= temp & 0x0f; // fill 4
-
- *code = (check_row & 0x01) << 7; // fill 1
- temp = *buffer++;
- check_row = temp ^ (temp >> 2);
- check_row ^= (check_row >> 1);
- *code |= temp >> 1; // fill 4
- *code &= 0xf8; // all 5
- *code |= (check_row & 0x10) >> 2; // fill 1
- *code++ |= (temp >> 2) & 0x03; // fill 2
-
- *code = temp << 6; // fill 2
- *code |= (check_row & 0x01) << 5; // fill 1
- *code |= check_col;
- }
- int main(int argc, char *argv[])
- {
- uint8_t buffer[5] = {0x30, 0x00, 0xf9, 0x49, 0x89};
- uint8_t res[8];
- code_create((const uint8_t*)buffer, (uint8_t*)res);
- printf("0x%02x
", res[0]);
- printf("0x%02x
", res[1]);
- printf("0x%02x
", res[2]);
- printf("0x%02x
", res[3]);
- printf("0x%02x
", res[4]);
- printf("0x%02x
", res[5]);
- printf("0x%02x
", res[6]);
- printf("0x%02x
", res[7]);
- return 0;
- }
复制代码Cfree+GCC测试通过!
QQ截图20151001020753.jpg (16.72 KB, 下载次数: 0)
下载附件
2015-10-1 02:05 上传
一周热门 更多>