Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

A problem with Binary to BCD conversion in C

Status
Not open for further replies.

Tulipmania

Newbie level 4
Joined
Jan 6, 2006
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,348
Hi,
I need help with this module . It's to convert a binary value to its BCD equivalent assigning each digit to an element of an array. For example the binary value (11111111=ff hex) is equivalent to( 255 ) decimal so the 5 digit is assigned to the first element of the array......second 5 is assigned to the next element and 2 is assigned to the third. the problem is that the module is able to assign only the first two digits and never gets to assign the third digit to the third element of the array.
I noted that while simulating the module..so what's wrong with it ?

the program;

#include<reg52.h>


void main (void)
{
int x; //Array Index
unsigned int binvalue=0x00ff; // Binary value to be converted & displayed
int INT_DIGITS[3]; // Declaration of Array of digits separated
int *INT_PTR; // Array Pointer

x=0; //Initializing the pointer to point the first element

do
{
INT_PTR = &INT_DIGITS[x];// set INT_PTR to point to indexed element in INT_DIGITS
*INT_PTR= (binvalue%10); // Assigning the ASCII value to the element
binvalue = binvalue/10;
++x;
}
while(binvalue >= 10);
}
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
637
Reputation
1,272
Reaction score
88
Trophy points
1,328
Location
USA
Activity points
33,178
Your "do" loop executes twice, not three times. Look at what's happening in the "while" test.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top