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