# convert hex to bcd in c

1. ## hex to bcd

how to convert a hex value to bcd using keil c

•

2. ## hex to bcd conversion in c

Salam,

You didn't mention what is the length of the hex 8 bit, 16 bits ...etc

Ok here is a simple method to convert hex (8bit) to bcd

One restriction that method is vaild fot numbers 0 - 99 only.

for example if you want to convert 99 to bcd

1- divide 99 by 10
2- put the result in the high nibble of the result variable
3- put the reminder in the low nibble of the result variable

This method is very easy to implement in C like this

Code:
```unsigned char hex2bcd (unsigned char x)
{
unsigned char y;
y = (x / 10) << 4;
y = y | (x % 10);
return (y);
}```
Where X is the Hex , and Y is the result

Enjoy

Bye

1 members found this post helpful.

•

3. ## hex to bcd conversion c code

From fmayr@electronic.tu-graz.ac.at Thu Feb 6 10:24:30 1997
Date: Wed, 05 Feb 1997 15:36:00 +0100
From: Friedrich Mayr <fmayr@electronic.tu-graz.ac.at>
To: 8051code@keil.com
Subject: Fast LONG to BCD conversion

This function was written within the Keil C51 environment
by Dr. Roehrer (Inst. f. Elektronik, TU-Graz) and me
to be called from a C - Program as: "void bin2bcd(void)"

The following global variable (must be located in the
internal RAM of the processor, "data" or "idata" ) is
used both to pass the argument (a "long" value) to the
function, as well as to receive the result (6 digits):

union
{
unsigned char buf[6];
long in;
} idata U_b;

The purpose of the code is to convert a signed long value
to BCD - code with leading sign if needed. Values exceeding
the range of 6(-:5) Digits should all be displayed by '-'.
The usage of memory was critical because no external RAM is
present in the system, so only the Accu, B, R1, R2, R3, R4
are used aside of the digit- (and argument-) buffer "U_b".

The trick applied in the function is to use a
"small" (nibble by nibble) division and the extensive
use of the '51 - opcodes XCHD and SWAP.
This results in very fast execution.
The function can easily be modified for example
to time-calculations or extended to the full "long"
range .
http://www.boerde.de/~matthias/m8051/bin2bcd.zip

•

4. ## hex to bcd in c

float a;
unsigned char b;

b = 0xff;
a = b;

5. ## Re: convert bcd to hex in c

// pass input 8 bit BCD format and conversion length, and get back hex values
// in output buffer.
void Bcd_To_Hex(unsigned char *Input_Buff, unsigned char *Output_Buff,unsigned char Len)
{ // by Neeraj Verma : neeraj.niet@gmail.com
unsigned char i,j;
for(i=0,j=0;i<Len;i++,j++)
{ if((i+1) >= Len)
{
if(*(Input_Buff+Len-1-i) >0x40)
*(Output_Buff+j) = (*(Input_Buff+Len-1-i)-0x37);
else
*(Output_Buff+j) = (*(Input_Buff+Len-1-i)-0x30);
}
else
{
if(*(Input_Buff+Len-1-i) >0x40)
{ if(*(Input_Buff+Len-2-i) >0x40)
{
*(Output_Buff+j) = ((*(Input_Buff+Len-i-2)-0x37)<<4) | (*(Input_Buff+Len-1-i)-0x37);
}
else
{
*(Output_Buff+j) = ((*(Input_Buff+Len-i-2)-0x30)<<4) | (*(Input_Buff+Len-1-i)-0x37);
}
}
else
{ if(*(Input_Buff+Len-2-i) >0x40)
{
*(Output_Buff+j) = ((*(Input_Buff+Len-i-2)-0x37)<<4) | (*(Input_Buff+Len-1-i)-0x30);
}
else
{
*(Output_Buff+j) = ((*(Input_Buff+Len-i-2)-0x30)<<4) | (*(Input_Buff+Len-1-i)-0x30);
}
}
}
i++;
}
}

•

6. ## Re: convert hex to bcd in c

/* Prototype */
void Hex2Decimal(unsigned long num);

/* Hex Variable in any register */
unsigned long num = 0x499602D2 ; /* 1234567890 */

int main (void)
{
Hex2Decimal(num); /* Convert given hex to decimal digits */

return 1; /* Good C!, not required */
}

/* Function */

/* Converts an unsigned hex number availabe in the micro registers to decimal digits
and stores the decimal digits in an array */
void Hex2Decimal(unsigned long num)
{
unsigned char value[10];
unsigned char i = 0;

while(num > 0)
{
value[i] = num % 10;
num /= 10;
i = i + 1;
}
}

--[[ ]]--