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;
}

//////////////////////////////////////////////////////////
//
//
Input: const unsigned char *hex hexadecimal data to be converted
//
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;
}

/////////////////////////////////////////////////////////
//
//
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;
}

/////////////////////////////////////////////////////////
//
//
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 = "";

unsigned char hex = {0x34, 0xFE, 0x3e, 0xFF};
unsigned long dec_hex = 0;

Dec_hex = Hextodec (hex, 4);

printf ("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 transcoding Decimal
unsigned long DEC_BCD = 0;
unsigned char bcd = {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;
}

//////////////////////////////////////////////////////////
//
//
Input: const unsigned char *hex hexadecimal data to be converted
//
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;
}

/////////////////////////////////////////////////////////
//
//
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;
}

/////////////////////////////////////////////////////////
//
//
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 = "";

unsigned char hex = {0x34, 0xFE, 0x3e, 0xFF};
unsigned long dec_hex = 0;

Dec_hex = Hextodec (hex, 4);

printf ("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 transcoding Decimal
unsigned long DEC_BCD = 0;
unsigned char bcd = {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 ();
}
Related Keywords:
Related Article