Hi ladies and gents,
Next exercise is the following:
Write the function which we can declare as follows:
unsigned char bcd (int n);
The idea of this function is to store the last two decimal numbers of n into a 'binary coded decimal' byte and to return this value. So, when n would be equal to 12345, then the 4 and the 5 from this number would be encoded into the 8 bits as:
0100 0101
Now, I tried to use the code Narue gave me, to cut the decimal number into pieces like this:
int main()
{
int a = 12345;
int x;
x = bcd(a);
cout << x <<endl;
return 0;
}
unsigned char bcd (int n)
{
int i;
char totvalue;
ostringstream out;
out<< n;
string s = out.str();
for (i = 1; i >= 1; --i)
{
totvalue = s[i] - '0';
start changing decimal number into binary code...
}
return totvalue;
}
THis way, I can get the separate values of that decimal number and then try to change them into a binary code?
Thing is, I'm not sure this is actually the correct/best way of dealing with this?
Questions are,
1) could someone show me wich is the best way to deal with this and keeping in mind that I have to use:
unsigned char bcd (int n);
2) Problem occurs is, that how can I return two separate pieces of binary code: first 0101 and then 0100 or is it 0100 and then 0101. Quiete frankly, I don't understand how this is accomplished. Or do you have to send it in one time 0100 0101?
3) Also, how can an unsigned char return a binary code 0100 0101?
Thanks for the help guys, maybe the solution is made out of the several exercises I allready made in wich you helped me aswell, but I just don't see how I can solve it :-|