Decimal, 16 binary, BCD Mutual

Source: Internet
Author: User
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 long) (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; 

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.