CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

casting problem?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
davt



Joined: 07 Oct 2003
Posts: 66
Location: England

View user's profile Send private message

casting problem?
PostPosted: Thu Nov 18, 2004 5:21 am     Reply with quote

Hi all


My problem is ' value ' will not accept anything over 255.

value is a unsigned int.

value=(int16)((hundreds*0x64)+(tens*0x0a)+(units));

Thanks for your help.

Dave
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Thu Nov 18, 2004 5:32 am     Reply with quote

That is because an unsigned int is only 8 bits wide

value should be declared as unsigned int16
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
davt



Joined: 07 Oct 2003
Posts: 66
Location: England

View user's profile Send private message

PostPosted: Thu Nov 18, 2004 5:36 am     Reply with quote

Thanks Andrew

Sorry I made a mistake - value is a unsigned int 16.

Thanks.

Dave
asmallri



Joined: 12 Aug 2004
Posts: 1634
Location: Perth, Australia

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Thu Nov 18, 2004 5:42 am     Reply with quote

how are the other variables declared?
_________________
Regards, Andrew

http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!!
davt



Joined: 07 Oct 2003
Posts: 66
Location: England

View user's profile Send private message

PostPosted: Thu Nov 18, 2004 5:47 am     Reply with quote

The other 3 values are declared as unsigned char.
Ttelmah
Guest







PostPosted: Thu Nov 18, 2004 6:17 am     Reply with quote

davt wrote:
The other 3 values are declared as unsigned char.

In which case your 'sum' should appear as:

value=((int16)hundreds*0x64)+((int16)tens*0x0a)+(units);

The problem is that you are taking an 8bit type, and an 8bit constant (this will be assumed, unless the constant if being involved in 16bit arithmetic, or has 'L' appended), and inherently then performing 8bit maths. Then at the end, you convert the result to an int16. You need to convert the individual 'values' to int16, thereby forcing 16bit arithmetic to be used.

Best Wishes
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Thu Nov 18, 2004 7:54 am     Reply with quote

For readability I would specify your constants in decimal. It makes no difference to the compiler or the resulting code, but is easier on the humans.
_________________
The search for better is endless. Instead simply find very good and get the job done.
davt



Joined: 07 Oct 2003
Posts: 66
Location: England

View user's profile Send private message

PostPosted: Thu Nov 18, 2004 10:50 am     Reply with quote

Thanks Ttelmah that did the trick!!
And sherpadoug - point taken! Smile
Regards.

Dave
Ttelmah
Guest







PostPosted: Thu Nov 18, 2004 11:02 am     Reply with quote

davt wrote:
Thanks Ttelmah that did the trick!!
And sherpadoug - point taken! Smile
Regards.

Dave

It is an area where great 'care' is needed, since the CCS compiler does not promote types forward in the way many C implementations do. The advantage of this, is that smaller code can be used by keeping the types small where required, but it prevents the simple automatic solution (on most C's, if you multiply two 8 bit types, and assign the result to a 16bit variable, the 'highest type' will be used, and the maths will be 16bit.
So long as you are aware of it (cast as necessary), the CCS solution makes a lot of sense for a microcontroller application.
Glad it is working now.
On Sherpadoug's comment, I have allways wondered if the 'number' in the Hitch-hikers guide to the galaxy, was actually an incorrect conversion from hex, and was trying to crack a joke on '2B or not 2B'. :-)
Binary makes 'sense', when coding for a 'mask', or a value fed to the individual bits on a port.
Hex makes sense when dealing with 'bytewide' values of a similar sort.
Decimal is by far the clearest when doing decimal conversions.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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