Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

hex to bcd

Status
Not open for further replies.

hawk

New Member
hi
any one tell me is correct or not or any simplex logic for hex to decimal

i want to do 25 hex value into decimal value

1. i multiply 5*1
2.then multiply 2*16
3.add both the value

but this is simple for 2digit if iam going for 4 digit its very complicated .
if anybody know simple logic plz sent to me

thanks for response
 

auburnate

New Member
the third digit you multiply by 256
the fourth digit you multiply by 4096

the pattern is 16^0 = 1

the pattern is 16^1 = 16

the pattern is 16^2 = 256

the pattern is 16^0 = 4096
 

DirtyLude

Well-Known Member
I had this exact same question recently.

I needed to display decimal digits on a 4 digit 7 segment display. The C code I finalized on:

//***************************
index = 4;

do
{
register unsigned short temp;

temp = val / 10;
display_digit( index, (char)(val - (temp * 10)));
val = temp;
index -= 1;

} while ( val > 0 );

/* Clear the unused part of the display.*/
while ( index >= 0 )
{
display_digit( index, 0x00 );
index -= 1;
}
 

StupidDum

New Member
This if the function i wrote. I store the decimal digit into an array. int n is the total decimal digit.

void convert_number(long int num, int *array[], int n)
{
long int remainder,quotient;
int i, j;

for (j=n-1;j>=0;j--)
{
quotient=num/10;
remainder=num%10;
array[j]=remainder+'0';
num=quotient;
if (!j) break;
}
}
 

Exo

Active Member
hawk said:
hi
any one tell me is correct or not or any simplex logic for hex to decimal

i want to do 25 hex value into decimal value

1. i multiply 5*1
2.then multiply 2*16
3.add both the value

but this is simple for 2digit if iam going for 4 digit its very complicated .
if anybody know simple logic plz sent to me

thanks for response

assuming you are using a PIC, take look on the piclist (www.piclist.com). There are routines for BCD conversion in assembly with very low cycle count. They do not use division, ... so they will be lots faster then the C routines above.
 

DirtyLude

Well-Known Member
Well, if you are looking for fast, here the ASM code for Hex to BCD with no division, but it's for the Z8e and I doubt it will help you.

Code:
Int2BCD10
;______________________________________________________________________________	
							;The actual BCD conversion loop
	rlc	r3					;Shift oiver the LB
	rlc	r2					;Now the HB

	adc	r1, r1				;Multiply by two
	da	r1

	adc	r0, r0				;HB too
	da	r0

	djnz	r4, Int2BCD10	;All sixteen bits

;_____________________________________________________________________________
							;Format and return the BCD
	ld		r2, 6(r15)		;Pointer to return array, usually 5(r15)

	ld		@r2, r0			;Store the HB, 1000s
	swap	@r2				;Reverse order
	and		@r2, #0FH		;Mask only lower nibble

	inc		r2				;Next digit
	ld		@r2, r0			;Store 100s
	and		@r2, #0FH		;Mask only lower nibble

	inc		r2				;Now LB
	ld		@r2, r1			;Store 10s
	swap		@r2			;Other order
	and		@r2, #0FH		;Mask only lower nibble

	inc		r2				;Point at 0nes
	ld		@r2, r1			;store ones
	and		@r2, #0FH		;Mask only lower nibble
 

phoenixrulez

New Member
Hex to Bcd for PIC16f877

Can anyone here give me the code to convert a 1 byte hex number to its bcd equivalent for the aforementioned uc?..i need it ASAP..also i would be thankful if the logic behind the program is mentioned..

PS: i need an Assembly language program and not a C-code..
 

ericgibbs

Well-Known Member
Most Helpful Member
Can anyone here give me the code to convert a 1 byte hex number to its bcd equivalent for the aforementioned uc?..i need it ASAP..also i would be thankful if the logic behind the program is mentioned..

PS: i need an Assembly language program and not a C-code..

hi,
Look here.
 
Status
Not open for further replies.

Latest threads

Top