哪种BCD?8421?还是余3循环? m1卡
可以这样设置一个数组,把对应表定义出来
比如8421BCD把
十进制数 8421BCD码
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
10 0001,0000 --> 压缩则是 1,0000 然后把你需要的日期看做INT型,每次%10求余数 然后/10 进位
对应数组存储,就可以了
// telnum.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
typedef unsigned long DWORD;
typedef unsigned short WORD;
typedef struct _tagTELNUM{
WORD WCountry;
WORD WRegion;
WORD WCodeseg;
WORD WOrdernum;
}TELNUM;
WORD CompressW(char *pn);
void CompressPN(TELNUM *ts,char *pn)//压缩16位电话号码
{
ts->WCountry=CompressW(pn);
ts->WRegion=CompressW(pn+4);
ts->WCodeseg=CompressW(pn+8);
ts->WOrdernum=CompressW(pn+12);
}
WORD CompressW(char *pn)//压缩4位电话号码部分
{
WORD cp;
cp=(pn[2]&0x0f)|(pn[3]<<4);
cp <= 8;
cp |=(WORD)(pn[0]&0x0f)|((pn[1]&0x0f)<<4);
return cp;
}
DWORD CompressDW(char*pn)//压缩8位电话号码部分
{
DWORD cp;
cp=CompressW(pn)|(CompressW(pn+4)<<16);
return cp;
}
char * DecompressPN( char *pn, TELNUM *ts)//解压缩16位号码
{
pn[0]=((ts->WCountry)&0x0f)|0x30;
pn[1]=(((ts->WCountry)>>4)&0x0f)|0x30;
pn[2]=(((ts->WCountry)>>8)&0x0f)|0x30;
pn[3]=(((ts->WCountry)>>12)&0x0f)|0x30;
pn[4]=((ts->WRegion)&0x0f)|0x30;
pn[5]=(((ts->WRegion)>>4)&0x0f)|0x30;
pn[6]=(((ts->WRegion)>>8)&0x0f)|0x30;
pn[7]=(((ts->WRegion)>>12)&0x0f)|0x30;
pn[8]=((ts->WCodeseg)&0x0f)|0x30;
pn[9]=(((ts->WCodeseg)>>4)&0x0f)|0x30;
pn[10]=(((ts->WCodeseg)>>8)&0x0f)|0x30;
pn[11]=(((ts->WCodeseg)>>12)&0x0f)|0x30;
pn[12]=((ts->WOrdernum)&0x0f)|0x30;
pn[13]=(((ts->WOrdernum)>>4)&0x0f)|0x30;
pn[14]=(((ts->WOrdernum)>>8)&0x0f)|0x30;
pn[15]=(((ts->WOrdernum)>>12)&0x0f)|0x30;
return pn;
}
char *DecompressW(char *pn, WORD w) //解压缩4位号码
{
for(int i=0;i<4;i++)
pn[i]=(char)(((w>>4*i)&0x0f)|0x30);
return pn;
}
char *DecompressDW(char *pn, DWORD dw) //解压缩8位号码
{
for(int i=0;i<8;i++)
pn[i]=(char)(((dw>>4*i)&0x0f)|0x30);
return pn;
}
int main(int argc, char* argv[])
{
char pn[17]="0086002412345678";
struct _tagTELNUM ts;
char dpn[16];
char dpn1[8];
char dpn2[4];
CompressPN(&ts,pn);//压缩16位电话号码
printf("%x,%x,%x,%x\n",ts.WCountry,ts.WRegion,ts.WCodeseg,ts.WOrdernum);
CompressDW(pn+8);//压缩8位号码段
printf("%x%x\n",ts.WOrdernum,ts.WCodeseg);
CompressW(pn);//压缩4位国家和地区号
printf("%x\n",ts.WCountry );
CompressW(pn+4);
printf("%x\n",ts.WRegion );
DecompressPN(dpn,&ts);//解压16位电话号码
for(int i=0;i<16;i++)
printf("%c",dpn[i]);
printf("\n");
DecompressDW(dpn1, CompressDW(pn+8));//解压8位号码段
for( i=0;i<8;i++)
printf("%c",dpn1[i]);
printf("\n");
DecompressW(dpn2,CompressW(pn));//解压4位国家和地区号
for(i=0;i<4;i++)
printf("%c",dpn2[i]);
printf("\n");
DecompressW(dpn2,CompressW(pn+4));
for(i=0;i<4;i++)
printf("%c",dpn2[i]);
printf("\n");
return 0;
}
BCD码、十六进制与十进制互转
在做嵌入式软件的设计中,经常会遇到十六进制、BCD码与十进制之间的转换,最近做M1卡的应用中,涉及了大量的十六进制、BCD码与十进制之间的转换。笔者通过对BCD码、十六进制 权的理解,轻松的实现了他们之间的互换。
#include <stdio.h>
#include <string.h>
/////////////////////////////////////////////////////
//
//功能:二进制取反
//
//输入:const unsigned char *src 二进制数据 // int length 待转换的二进制数据长度
//
//输出:unsigned char *dst 取反后的二进制数据
//
//返回:0 success
/
/
//////////////////////////////////////////////////////
int convert(unsigned char *dst, const unsigned char *src, int length)
{
int i;
for(i=0; i<length; i++)
{
dst[i] = src[i]^0xFF;
}
return 0;
}
/
/////////////////////////////////////////////////////////
//
//功能:十六进制转为十进制
//
//输入:const unsigned char *hex 待转换的十六进制数据
// int length 十六进制数据长度
//
//输出:
//
//返回:int rslt 转换后的十进制数据
//
/
/思路:十六进制每个字符位所表示的十进制数的范围是0 ~255,进制为256
// 左移8位(<<8)等价乘以256
//
/////////////////////////////////////////////////////////
unsigned long HextoDec(const unsigned char *hex, int length)
{
int i;
unsigned long rslt = 0;
for(i=0; i<length; i++)
{
rslt += (unsigned long)(hex[i])<<(8*(length-1-i));
}
return rslt;
}
/////////////////////////////////////////////////////////
//
//功能:十进制转十六进制
//
//输入:int dec 待转换的十进制数据
// int length 转换后的十六进制数据长度
//
//输出:unsigned char *hex 转换后的十六进制数据
/
/
//返回:0 success
//
//思路:原理同十六进制转十进制
//////////////////////////////////////////////////////////
int DectoHex(int dec, unsigned char *hex, int length)
{
int i;
for(i=length-1; i>=0; i--)
{
hex[i] = (dec%256)&0xFF;
dec /= 256;
}
return 0;
}
/////////////////////////////////////////////////////////
//
//功能:求权
//
//输入:int base 进制基数
// int times 权级数
//
/
/输出:
//
//返回:unsigned long 当前数据位的权
//
//////////////////////////////////////////////////////////
unsigned long power(int base, int times)
{
int i;
unsigned long rslt = 1;
for(i=0; i<times; i++)
rslt *= base;
return rslt;
}
/////////////////////////////////////////////////////////
//
//功能:BCD转10进制
//
//输入:const unsigned char *bcd 待转换的BCD码
// int length BCD码数据长度
//
//输出:
//
/
/返回:unsigned long 当前数据位的权
//
//思路:压缩BCD码一个字符所表示的十进制数据范围为0 ~ 99,进制为100
// 先求每个字符所表示的十进制值,然后乘以权
//////////////////////////////////////////////////////////
unsigned long BCDtoDec(const unsigned char *bcd, int length)
{
int i, tmp;
unsigned long dec = 0;
for(i=0; i<length; i++)
{
tmp = ((bcd[i]>>4)&0x0F)*10 + (bcd[i]&0x0F);
dec += tmp * power(100, length-1-i);
}
return dec;
}
/////////////////////////////////////////////////////////
//
//功能:十进制转BCD码
//
//输入:int Dec 待转换的十进制数据
// int length BCD码数据长度
/
/
//输出:unsigned char *Bcd 转换后的BCD码
//
//返回:0 success
//
//思路:原理同BCD码转十进制
//
//////////////////////////////////////////////////////////
int DectoBCD(int Dec, unsigned char *Bcd, int length)
{
int i;
int temp;
for(i=length-1; i>=0; i--)
{
temp = Dec%100;
Bcd[i] = ((temp/10)<<4) + ((temp%10) & 0x0F);
Dec /= 100;
}
return 0;
}
int main()
{
register int i;
unsigned char tmp_bff[12] = "";
//十六进制转十进制
unsigned char HEX[4] = {0x34, 0xFE, 0x3E, 0xFF};
unsigned long dec_hex = 0;
dec_hex = HextoDec(HEX, 4);
p
rintf("dec_hex = %d\n", dec_hex);
//十进制转十六进制
DectoHex(dec_hex, tmp_bff, 4);
for(i=0; i<5; i++)
{
printf("tmp_bff[%d] = 0x%02X\n",i, tmp_bff[i]);
}
//BCD码转十进制
unsigned long dec_bcd = 0;
unsigned char BCD[4] = {0x98, 0x23, 0x45, 0x78};
dec_bcd = BCDtoDec(BCD, 4);
printf("dec_bcd = %d\n", dec_bcd);
//十进制转BCD码
DectoBCD(dec_bcd, tmp_bff, 4);
for(i=0; i<5; i++)
{
printf("tmp_bff[%d] = 0x%02X\n", i, tmp_bff[i]);
}
getchar();
}
BCD
void BCDToChar(const unsigned char *src,unsigned int srcLen, unsigned char *dest)
{
unsigned char temp[2] = {0};
unsigned char temp2[1] = {0};
for(int i=0; i<srcLen; i++)
{
temp2[0] = src[i];
temp[0] = src[i]>>4;
temp[0] = temp[0]&0x0F;
temp[0] = temp[0]+0x30;
temp[1] = temp2[0]&0x0F;
temp[1] = temp[1]+0x30;
dest[i*2] = temp[0];
dest[i*2+1] = temp[1];
}
}