Sep 10, 2014 at 1:25am UTC
Hi I'm having trouble converting a 4 digit number into a BCD number, in the program I did below I was able to convert a 2 digit number into BCD, but I do not know how to convert a 4 digit number or how to start it.
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string asciiNum;
char digMost, digLeast;
int packBCD, intBCD;
cout << "Enter a two-digit number : " ;
cin >> asciiNum;
printf("\n\nMost sig. ascii digit = %x Least sig. ascii digit = %x\n\n", asciiNum[0], asciiNum[1]);
digMost = asciiNum[0] & 0x0f;
digLeast = asciiNum[1] & 0x0f;
printf("Most sig. bcd digit = %x Least sig. bcd digit = %x\n\n", digMost, digLeast);
packBCD = 0;
packBCD = packBCD | digMost;
packBCD = packBCD << 4;
packBCD = packBCD | digLeast;
intBCD = packBCD;
printf("Packed BCD value = %x \n\n", packBCD);
printf("int BCD = %d\n\n", intBCD);
system("PAUSE");
return EXIT_SUCCESS;
}
Sep 10, 2014 at 1:56am UTC
I'm sorry that I did not use the code tags when in my previous post it I forgot to format it before posting.
I revised my program with the loop from what I had and it worked out. I did not think of using a loop.
Thank You!