Anscii Code and BCD code for mutual transfer

Source: Internet
Author: User

BOOL Atobcd (unsigned char* asc,unsigned char* bcd,int len)
{
int i;
unsigned char ch; High
unsigned char cl; Low
unsigned char temp1;
unsigned char temp2;
unsigned char *retrun;

Retrun = new unsigned char[len/2+1];

memset (retrun,0,len/2+1);

for (i=0;i<len/2;i++)
{
Temp1 = Asc[2*i]; High
Temp2 = asc[2*i+1]; Low

if (Temp1 >= ' A ' && temp1 <= ' F ') | | (Temp1 >= ' a ' && temp1 <= ' F '))
{
ch = (((temp1-7) & 0x0f) <<4) & 0xFF;
}
else if (Temp1 >= ' 0 ' && temp1 <= ' 9 ')
{
ch = ((Temp1 & 0x0f) <<4) & 0xFF;
}
Else
{
return false;
}

//*************************************************************************//

if (temp2 >= ' A ' && temp2 <= ' F ') | | (Temp2 >= ' a ' && temp2 <= ' F '))
{
CL = ((temp2-7) & 0x0f) & 0xFF;
}
else if (Temp2 >= ' 0 ' && temp2 <= ' 9 ')
{
CL = (Temp2 & 0x0f) & 0xFF;
}
Else
{
return false;
}

Retrun[i] = ch | Cl
}

memcpy (BCD,RETRUN,LEN/2);
delete []retrun;

return true;
}

unsigned char *bcdtoa (unsigned char * Data, int Len)
{
int i,j;
unsigned char temp1,temp2;
unsigned char ptr[500];
unsigned char *ptr;
j=0;

for (i=0;i<len;i++)
{
Temp1=data[i];
TEMP2=TEMP1 & 0x0f;
if ((temp2>=0x00) && (temp2<=0x09))
{
ptr[i*2+1]=temp2+0x30;
}
Else
{
if (temp2==10)
ptr[i*2+1]= ' A ';
else if (temp2==11)
ptr[i*2+1]= ' B ';
else if (temp2==12)
ptr[i*2+1]= ' C ';
else if (temp2==13)
ptr[i*2+1]= ' D ';
else if (temp2==14)
ptr[i*2+1]= ' E ';
else if (temp2==15)
ptr[i*2+1]= ' F ';
}

Temp1=data[i];
temp2=temp1>>4;
TEMP2=TEMP2 & 0x0f;

if ((temp2>=0x00) && (temp2<=0x09))
{
ptr[i*2]=temp2+0x30;
}
Else
{
if (temp2==10)
ptr[i*2]= ' A ';
else if (temp2==11)
ptr[i*2]= ' B ';
else if (temp2==12)
ptr[i*2]= ' C ';
else if (temp2==13)
ptr[i*2]= ' D ';
else if (temp2==14)
ptr[i*2]= ' E ';
else if (temp2==15)
ptr[i*2]= ' F ';

}
}
ptr = ptr;
return (PTR);
}

Anscii Code and BCD code for mutual transfer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.