| | |
Exercise using: unsigned char bcd(int n);
![]() |
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:
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:
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 :-|
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:
C Syntax (Toggle Plain Text)
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:
C Syntax (Toggle Plain Text)
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 :-|
>out<< n;
>string s = out.str();
In this case (since you're taking the rightmost digits) arithmetic operations may be easier:
>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:
>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.
>string s = out.str();
In this case (since you're taking the rightmost digits) arithmetic operations may be easier:
C Syntax (Toggle Plain Text)
if ( n >= 10 ) { // Make sure there are two digits int a = n % 10; int b = ( n / 10 ) % 10; }
It's an exercise in bit packing. The two 4 bit chunks are packed together in an unsigned char:
C Syntax (Toggle Plain Text)
( b << 4 ) | a
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.
Well, thanks for the help Narue,
With the code you showed, I managed to make this:
Problem is that alltough I'm not getting any error messages, when I debug the program and get towards this part of the program:
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
With the code you showed, I managed to make this:
C Syntax (Toggle Plain Text)
unsigned char bcd (int n); int main() { int value = 12345; int x; x = bcd(value); cout << x <<endl; return 0; } unsigned char bcd (int n) { char binary[80], temp[80], code; int remain = 0, decimal = 0; int a, b, i, k = 0; if (n >= 10) { a = n % 10; b = (n / 10) % 10; } for (i = 0; i < 2; ++i) { if (i == 0) decimal = a; else decimal = b; do { remain = decimal % 2; decimal = decimal / 2; temp[k++] = remain + 0x30; } while (decimal > 0); while (k >= 0) { binary[n++] = temp[--k]; } binary[n-1] = 0; code = ( binary[n] << 4 ) | binary[n]; } return code; }
Problem is that alltough I'm not getting any error messages, when I debug the program and get towards this part of the program:
C Syntax (Toggle Plain Text)
binary[n++] = temp[--k];
I'm assuming I did something seriously wrong here, tough it doesn't crash the program
>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:
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:
C Syntax (Toggle Plain Text)
unsigned char bcd ( int n ) { if ( n >= 10 ) { // Make sure there are two digits int a = n % 10; int b = ( n / 10 ) % 10; return ( b << 4 ) | a; } return n; }
I'm here to prove you wrong.
>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:
You're confusing a binary code with the string representation of 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:
C Syntax (Toggle Plain Text)
while ( value != 0 ) { cout<< !!( value & 1 ); value >>= 1; }
I'm here to prove you wrong.
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. Or am I just off today?
#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]
*/ "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
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
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
>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.
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.
http://en.wikipedia.org/wiki/Binary_coded_decimal
C Syntax (Toggle Plain Text)
Decimal Binary Coded Decimal BCD (Hex) BCD (Decimal) 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
![]() |
Other Threads in the C Forum
- Previous Thread: infinite loop
- Next Thread: Combinations(N choose R) ?
| Thread Tools | Search this Thread |
* ansi api array arrays binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






