Conversion between 16 and 10 binary BCD codes

Source: Internet
Author: User
#include <stdio.h>
#include <string.h>

/////////////////////////////////////////////////////
//
function: Binary Reverse
//
Input: const unsigned char *src binary data
int length to be converted binary data
//
Output: unsigned char *dst binary data after inversion
//
Return: 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;
}

//////////////////////////////////////////////////////////
//
function: Hexadecimal to decimal
//
Input: const unsigned char *hex hexadecimal data to be converted
int length hexadecimal data len
//
Output:
//
Returns: int rslt converted decimal data
//
Train of thought: hexadecimal the range of decimal digits represented by each character bit is 0 ~255, and the system is 256.
Left 8-bit (<<8) equivalent multiplied by 256
//
/////////////////////////////////////////////////////////
unsigned long hextodec (const unsigned char *hex, int length)
{
int i;
unsigned long rslt = 0;

for (i=0; i<length; i++)
{
Rslt + + (unsigned int) (Hex[i]) << (8* (length-1-i));

}

return rslt;
}


/////////////////////////////////////////////////////////
//
function: decimal hexadecimal
//
Input: int Dec decimal data for conversion
Length of the hexadecimal data after the int length conversion
//
Output: Hexadecimal data converted by unsigned char *hex
//
Return: 0 Success
//
Train of thought: Principle with hexadecimal turn decimal
//////////////////////////////////////////////////////////
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;
}

/////////////////////////////////////////////////////////
//
Function: the right to ask
//
Input: int base into base
Int times Right Series
//
Output:
//
Return: unsigned long the current data bit right
//
//////////////////////////////////////////////////////////
unsigned long (int base, int times)
{
int i;
unsigned long rslt = 1;

for (i=0; i<times; i++)
Rslt *= Base;

return rslt;
}

/////////////////////////////////////////////////////////
//
Function: BCD 10 system
//
Input: const unsigned char *BCD BCD code to convert
int length BCD code data lengths
//
Output:
//
Return: unsigned long the current data bit right
//
Idea: Compress BCD Code a character represents a decimal data range of 0 ~ 99, the system is 100
First ask for the decimal value represented by each character, then multiply the right
//////////////////////////////////////////////////////////
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 (MB, length-1-i);
}

return Dec;
}

/////////////////////////////////////////////////////////
//
Function: Decimal Conversion BCD Code
//
Input: int DEC decimal data for conversion
int length BCD code data lengths
//
Output: unsigned char *BCD converted BCD Code
//
Return: 0 Success
//
Train of thought: Principle with BCD code turn decimal
//
//////////////////////////////////////////////////////////
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] = "";

Hexadecimal decimal
unsigned char hex[4] = {0x34, 0xFE, 0x3e, 0xFF};
unsigned long dec_hex = 0;

Dec_hex = Hextodec (hex, 4);

printf ("Dec_hex =%d\n", Dec_hex);

Decimal turn hexadecimal
Dectohex (Dec_hex, TMP_BFF, 4);
for (i=0; i<5; i++)
{
printf ("tmp_bff[%d] = 0x%02x\n", I, tmp_bff[i]);
}

BCD transcoding Decimal
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);

Decimal Conversion BCD Code
DECTOBCD (DEC_BCD, TMP_BFF, 4);
for (i=0; i<5; i++)
{
printf ("tmp_bff[%d] = 0x%02x\n", I, tmp_bff[i]);
}

GetChar ();
}
April 30
Conversion between 16, 10, BCD
#include <stdio.h>
#include <string.h>

/////////////////////////////////////////////////////
//
function: Binary Reverse
//
Input: const unsigned char *src binary data
int length to be converted binary data
//
Output: unsigned char *dst binary data after inversion
//
Return: 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;
}

//////////////////////////////////////////////////////////
//
function: Hexadecimal to decimal
//
Input: const unsigned char *hex hexadecimal data to be converted
int length hexadecimal data len
//
Output:
//
Returns: int rslt converted decimal data
//
Train of thought: hexadecimal the range of decimal digits represented by each character bit is 0 ~255, and the system is 256.
Left 8-bit (<<8) equivalent multiplied by 256
//
/////////////////////////////////////////////////////////
unsigned long hextodec (const unsigned char *hex, int length)
{
int i;
unsigned long rslt = 0;

for (i=0; i<length; i++)
{
Rslt + + (unsigned int) (Hex[i]) << (8* (length-1-i));

}

return rslt;
}


/////////////////////////////////////////////////////////
//
function: decimal hexadecimal
//
Input: int Dec decimal data for conversion
Length of the hexadecimal data after the int length conversion
//
Output: Hexadecimal data converted by unsigned char *hex
//
Return: 0 Success
//
Train of thought: Principle with hexadecimal turn decimal
//////////////////////////////////////////////////////////
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;
}

/////////////////////////////////////////////////////////
//
Function: the right to ask
//
Input: int base into base
Int times Right Series
//
Output:
//
Return: unsigned long the current data bit right
//
//////////////////////////////////////////////////////////
unsigned long (int base, int times)
{
int i;
unsigned long rslt = 1;

for (i=0; i<times; i++)
Rslt *= Base;

return rslt;
}

/////////////////////////////////////////////////////////
//
Function: BCD 10 system
//
Input: const unsigned char *BCD BCD code to convert
int length BCD code data lengths
//
Output:
//
Return: unsigned long the current data bit right
//
Idea: Compress BCD Code a character represents a decimal data range of 0 ~ 99, the system is 100
First ask for the decimal value represented by each character, then multiply the right
//////////////////////////////////////////////////////////
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 (MB, length-1-i);
}

return Dec;
}

/////////////////////////////////////////////////////////
//
Function: Decimal Conversion BCD Code
//
Input: int DEC decimal data for conversion
int length BCD code data lengths
//
Output: unsigned char *BCD converted BCD Code
//
Return: 0 Success
//
Train of thought: Principle with BCD code turn decimal
//
//////////////////////////////////////////////////////////
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] = "";

Hexadecimal decimal
unsigned char hex[4] = {0x34, 0xFE, 0x3e, 0xFF};
unsigned long dec_hex = 0;

Dec_hex = Hextodec (hex, 4);

printf ("Dec_hex =%d\n", Dec_hex);

Decimal turn hexadecimal
Dectohex (Dec_hex, TMP_BFF, 4);
for (i=0; i<5; i++)
{
printf ("tmp_bff[%d] = 0x%02x\n", I, tmp_bff[i]);
}

BCD transcoding Decimal
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);

Decimal Conversion BCD Code
DECTOBCD (DEC_BCD, TMP_BFF, 4);
for (i=0; i<5; i++)
{
printf ("tmp_bff[%d] = 0x%02x\n", I, tmp_bff[i]);
}

GetChar ();
}

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.