Exercise using: unsigned char bcd(int n);

Reply

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Exercise using: unsigned char bcd(int n);

 
0
  #1
Mar 31st, 2005
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:

  1. int main()
  2. {
  3. int a = 12345;
  4. int x;
  5.  
  6. x = bcd(a);
  7. cout << x <<endl;
  8.  
  9. return 0;
  10. }
  11.  
  12. unsigned char bcd (int n)
  13. {
  14. int i;
  15. char totvalue;
  16. ostringstream out;
  17.  
  18. out<< n;
  19. string s = out.str();
  20.  
  21. for (i = 1; i >= 1; --i)
  22. {
  23. totvalue = s[i] - '0';
  24. start changing decimal number into binary code...
  25. }
  26. return totvalue;
  27. }

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:
  1. 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 :-|
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Exercise using: unsigned char bcd(int n);

 
0
  #2
Mar 31st, 2005
>out<< n;
>string s = out.str();
In this case (since you're taking the rightmost digits) arithmetic operations may be easier:
  1. if ( n >= 10 ) { // Make sure there are two digits
  2. int a = n % 10;
  3. int b = ( n / 10 ) % 10;
  4. }
>how can I return two separate pieces of binary code
It's an exercise in bit packing. The two 4 bit chunks are packed together in an unsigned char:
  1. ( b << 4 ) | a
>3) Also, how can an unsigned char return a binary code 0100 0101?
The char type has to be at least 8 bits by definition, so you can easily fit two 4 bit pieces there.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Exercise using: unsigned char bcd(int n);

 
0
  #3
Mar 31st, 2005
Well, thanks for the help Narue,

With the code you showed, I managed to make this:
  1. unsigned char bcd (int n);
  2.  
  3. int main()
  4. {
  5. int value = 12345;
  6. int x;
  7.  
  8. x = bcd(value);
  9. cout << x <<endl;
  10.  
  11. return 0;
  12. }
  13.  
  14. unsigned char bcd (int n)
  15. {
  16. char binary[80], temp[80], code;
  17. int remain = 0, decimal = 0;
  18. int a, b, i, k = 0;
  19.  
  20. if (n >= 10)
  21. {
  22. a = n % 10;
  23. b = (n / 10) % 10;
  24. }
  25.  
  26. for (i = 0; i < 2; ++i)
  27. {
  28. if (i == 0)
  29. decimal = a;
  30. else
  31. decimal = b;
  32.  
  33. do
  34. {
  35. remain = decimal % 2;
  36. decimal = decimal / 2;
  37. temp[k++] = remain + 0x30;
  38. }
  39. while (decimal > 0);
  40.  
  41. while (k >= 0)
  42. {
  43. binary[n++] = temp[--k];
  44. }
  45. binary[n-1] = 0;
  46.  
  47. code = ( binary[n] << 4 ) | binary[n];
  48. }
  49.  
  50. return code;
  51. }

Problem is that alltough I'm not getting any error messages, when I debug the program and get towards this part of the program:
  1. binary[n++] = temp[--k];
I get a message: Unhandled exception in LAOef4_6.exe: 0xC0000005: Access Violation :o

I'm assuming I did something seriously wrong here, tough it doesn't crash the program
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Exercise using: unsigned char bcd(int n);

 
0
  #4
Mar 31st, 2005
>0xC0000005: Access Violation
An access violation means you tried to access memory outside of your address space. After debugging lots of these problems, you begin to get a sense for what memory addresses look "wrong" for what you're trying to do. 0xC0000005 looks wrong, and most likely one of your indices is accessing an array out of bounds.

Of course, your solution is way too long. You could practically paste the code I gave you into your function body and be set:
  1. unsigned char bcd ( int n )
  2. {
  3. if ( n >= 10 ) { // Make sure there are two digits
  4. int a = n % 10;
  5. int b = ( n / 10 ) % 10;
  6.  
  7. return ( b << 4 ) | a;
  8. }
  9.  
  10. return n;
  11. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Exercise using: unsigned char bcd(int n);

 
0
  #5
Mar 31st, 2005
>Of course, your solution is way too long. You could practically paste the code I gave you into your function body and be set:

Well, I did use your code, but using it solely doesn't make the decimal number into a binary code, that's why I used the remaining code?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Exercise using: unsigned char bcd(int n);

 
0
  #6
Mar 31st, 2005
>but using it solely doesn't make the decimal number into a binary code
The binary code is packed inside an unsigned char. The only way to get it out is to access the value bit by bit:
  1. while ( value != 0 ) {
  2. cout<< !!( value & 1 );
  3. value >>= 1;
  4. }
You're confusing a binary code with the string representation of a binary code.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,319
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Exercise using: unsigned char bcd(int n);

 
0
  #7
Mar 31st, 2005
I was under the impression that the return value might be expected to be an unsigned int, and that the expected input/output might be as follows.
#include <stdio.h>

unsigned int dec2bcd(unsigned int dec)
{
   unsigned int bits, bcd = 0;
   for ( bits = 0; dec; bits += 4, dec /= 10 )
   {
      bcd |= dec % 10 << bits;
   }
   return bcd;
}

int main()
{
   unsigned int value = 12345, result = dec2bcd(value);
   printf("bcd(%u) = 0x%X [%u]\n", value, result, result);
   return 0;
}

/* my output
bcd(12345) = 0x12345 [74565]
*/
Or am I just off today?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Exercise using: unsigned char bcd(int n);

 
0
  #8
Mar 31st, 2005
Dave and especially Narue,

Thanks for the help, but I'm giving up on this, I simply am not getting what is asked in this exercise and the pieces of code you are giving me Narue are just confusing me more and more and instead of leading me to the solution is getting me further and further away :!:

To say it as it is, I'm just not cut out for this, I hoped to learn this as a fun hobby, but actually is getting me frustrated as it's not working out as it should.

STill, you guys are top notch and I enjoyed learning from you tough I didn't remember all of it and certainly so it seems didn't understand all of it
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Exercise using: unsigned char bcd(int n);

 
0
  #9
Mar 31st, 2005
>but I'm giving up on this
Not totally I hope. You've shown a lot of promise. Programming is hard. There's no getting around it, so you just have to keep going until you finally have a flash of insight and understand what was confusing you. Then you can move on to a new thing that confuses you. This process is repeated until you die.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,319
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 230
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Exercise using: unsigned char bcd(int n);

 
0
  #10
Mar 31st, 2005
http://en.wikipedia.org/wiki/Binary_coded_decimal
  1. Decimal Binary Coded Decimal BCD (Hex) BCD (Decimal)
  2. 12345 -> 0001 0010 0011 0100 0101 0x12345 74565
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC