|
|
View previous topic :: View next topic |
Author |
Message |
cool121
Joined: 16 Mar 2011 Posts: 10 Location: US
|
[Request] 9's complement to decimal |
Posted: Sun May 15, 2011 12:36 am |
|
|
Can somebody give me a code that translate the 9's complement of a number to its decimal number? The range of numbers are from -40,000 to +40,000. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Sun May 15, 2011 3:15 am |
|
|
How are the numbers arriving?.
Is this ASCII, BCD, or what?.
Are you sure the arriving value really is +/- 40000, or is this the range of numbers represented?.
Generally converting from 9's complement, is easy. You just take each digit in turn, and subtract it from 9. There normally wouldn't be a sign though. The whole 'point' of using 9's complement, is so that you can perform a subtraction, by simply adding a number to the 9's complement of the number we want to subtract, add one, and ignore the carry.
So just as in two's complement (used for the basic arithmetic in the PIC), you can perform subtraction with an addition
You need to give us more details of the actual format involved, but the simple conversion to 9's complement (working with int32), assuming the source numbers are always 5 digits, is:
Code: |
int32 val_to_convert=35654;
int32 temp=99999; //10^5 -1
temp-=val_to_convert; //temp is now the 9's complement of 35654
|
So I'd actually guess you have a five digit number arriving _without a sign_, representing the number range -40000 to +40000, with only the negative values complemented. The reverse conversion in this case would be:
Code: |
int32 arriving_value = 64354;
signed int32 temp; //must be signed
if (arriving_value>=50000) {
//here value is a complement representing a -ve number
temp=arriving_value-99999;
}
else {
temp=arriving_value;
}
|
Best Wishes |
|
|
cool121
Joined: 16 Mar 2011 Posts: 10 Location: US
|
|
Posted: Sun May 15, 2011 9:43 am |
|
|
Ttelmah wrote: | How are the numbers arriving?.
Is this ASCII, BCD, or what?.
Are you sure the arriving value really is +/- 40000, or is this the range of numbers represented?.
|
Numbers arrive in BCD format. And it also say that last BCD for "digital auto-zero" what is the meaning of that?
Yes the negative numbers are arrived in complement format.
Thank you |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|