最近在研究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
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
;**********************************************
; subroutine:
; name: Convert_Hex2EMcode
; function:
; convert hex data to em code
; input:
; gvUserBuf
; output:
; gvBuffer
;**********************************************
Convert_Hex2EMcode:
mov gvBuffer,#0ffh
mov r0,#gvBuffer+1
mov @r0,#01h
mov r1,#gvUserBuf
mov a,@r1
mov b,a
mov bitcount,#07h
mov bytecount,#05h
mov r4,#04h
ConvertEMcode_Loop1:
mov a,b
rlc a
mov b,a
mov a,@r0
rlc a
mov @r0,a
djnz bitcount,ConvertEMcode_Loop11
mov bitcount,#08h
inc r0
ConvertEMcode_Loop11:
djnz r4,ConvertEMcode_Loop1
mov a,@r1
anl a,#0f0h
swap a
mov c,PSW.0
mov a,@r0
rlc a
mov @r0,a
djnz bitcount,ConvertEMcode_Loop12
mov bitcount,#08h
inc r0
ConvertEMcode_Loop12:
mov r4,#04h
ConvertEMcode_Loop2:
mov a,b
rlc a
mov b,a
mov a,@r0
rlc a
mov @r0,a
djnz bitcount,ConvertEMcode_Loop21
mov bitcount,#08h
inc r0
ConvertEMcode_Loop21:
djnz r4,ConvertEMcode_Loop2
mov a,@r1
anl a,#0fh
mov c,PSW.0
mov a,@r0
rlc a
mov @r0,a
djnz bitcount,ConvertEMcode_Loop22
mov bitcount,#08h
inc r0
ConvertEMcode_Loop22:
inc r1
mov a,@r1
mov b,a
mov r4,#04h
djnz bytecount,ConvertEMcode_Loop1
mov a,gvBuffer+7
swap a
rlc a
mov gvBuffer+7,a
ConvertEMcode_Loop3:
mov r1,#gvUserBuf
mov r3,#00h
mov r5,#05h
ConvertEMcode_Loop31:
mov a,@r1
swap a
anl a,#0fh
xrl a,r3
mov r3,a
mov a,@r1
anl a,#0fh
xrl a,r3
mov r3,a
inc r1
djnz r5,ConvertEMcode_Loop31 ; get colume parity
clr c ; last bit is zero
mov a,r3
rlc a
mov r3,a ; get last parity
mov a,@r0
anl a,#11100000b
orl a,r3
mov @r0,a
ret
- #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 上传
多谢各位,昨晚我也搞掂了,也是有很多行语句呀
void clqo()//行列奇偶位计算 并将五个字节的十六进制转为卡的9个头及奇偶位共64位即 八个字节的十六进制数据
{
uchar kk;
for(kk = 0;kk < 5;kk++)//将五个字节的十六进制分开半个字节
{
Buf_RF_Data[kk * 2 + 0] = ID_Code[kk] >> 4;
Buf_RF_Data[kk * 2 + 1] = ID_Code[kk] & 0x0F;
}
for(kk = 0;kk < 10;kk++)//计算每半个字节的奇偶位
{
Buf_RF_Data[kk] &= 0x0F; //先屏蔽高4位
if(((Buf_RF_Data[kk] >> 3) + (Buf_RF_Data[kk] >> 2) + (Buf_RF_Data[kk] >> 1) + (Buf_RF_Data[kk] & 0x01)) % 2)
{
Buf_RF_Data[kk] <<= 1;
Buf_RF_Data[kk] |= 1;
}
else //为偶数个 1 则补 0
{
Buf_RF_Data[kk] <<= 1;
}
}
for(kk = 4;kk > 0;kk--)//列奇偶位
{
Buf_RF_Data[kk] &= 0x1F; //先屏蔽高3位
if(((Buf_RF_Data[0] >> kk) + (Buf_RF_Data[1] >> kk) + (Buf_RF_Data[2] >> kk) + (Buf_RF_Data[3] >> kk) + (Buf_RF_Data[4] >> kk)
+ (Buf_RF_Data[5] >> kk) + (Buf_RF_Data[6] >> kk) + (Buf_RF_Data[7] >> kk) + (Buf_RF_Data[8] >> kk) + (Buf_RF_Data[9] >> kk)) % 2)
{
Buf_RF_Data[10] |= 1;
Buf_RF_Data[10] <<= 1;
}
else //为偶数个 1 则补 0
{
Buf_RF_Data[10] <<= 1;
}
}
Buf_RF_Data[0] <<= 2;
Buf_RF_Data[0] |= 0x80;
Buf_RF_Data[0] |= Buf_RF_Data[1] >> 3;
Buf_RF_Data[1] <<= 5;
Buf_RF_Data[1] |= Buf_RF_Data[2];
Buf_RF_Data[2] <<= 3;
Buf_RF_Data[2] |= Buf_RF_Data[4] >> 2;
Buf_RF_Data[3] = Buf_RF_Data[4] << 6;
Buf_RF_Data[3] |= Buf_RF_Data[5] << 1;
Buf_RF_Data[3] |= Buf_RF_Data[6] >> 4;
Buf_RF_Data[4] = Buf_RF_Data[6] << 4;
Buf_RF_Data[4] |= Buf_RF_Data[7] >> 1;
Buf_RF_Data[5] = Buf_RF_Data[7] << 7;
Buf_RF_Data[5] |= Buf_RF_Data[8] << 2;
Buf_RF_Data[5] |= Buf_RF_Data[9] >> 3;
Buf_RF_Data[6] = Buf_RF_Data[9] << 5;
Buf_RF_Data[6] |= Buf_RF_Data[10];
for(kk = 7;kk > 0;kk--)
{
Buf_RF_Data[kk] = Buf_RF_Data[kk - 1];
}
Buf_RF_Data[0] = 0xFF; //9个头中的前8个1
Buf_RF_Data[8] = Buf_RF_Data[9] = Buf_RF_Data[10] = 0;//这个可以不清 0
}
计算奇偶校验用异或更快更省空间
一周热门 更多>