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 CCS Technical Support

array to long conversion

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



Joined: 06 May 2008
Posts: 14

View user's profile Send private message

array to long conversion
PostPosted: Mon Feb 27, 2017 1:37 am     Reply with quote

Hi;

I am getting currect data from serial port and i save it in array of 3.
i.e:
int array_display[3];

now i want to convert it to long int.
array_display[0] will be save on unit place of long int
array_display[1] will be save on ten place of long int &
array_display[2] will be save on hundred place of long int

Example:
if array_display[0] = 3 , array_display[1] = 4 & array_display[2] = 5 then
long int new_data should be 543.

& i used the following formula but it is not working.
new_data = array_display[0] + array_display[1]*10 + array_display[2]*100;

what is wrong with the method. Any Idea?
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Mon Feb 27, 2017 1:58 am     Reply with quote

Critical change:

new_data = array_display[0] + array_display[1]*10 + array_display[2]*100L;

Note the 'L'.

Problem is that each 'array_display' value is an int8. '100' is also an int8. 5*100 using int8 maths, overflows and gives you 244.

Only the 'hundreds' can overflow this way (9*10 = 90 is still small enough to fit in an int8). Adding the 'L' makes the constant '100', into a 'long' (int16). So int16 arithmetic is then used here. Because int16 is then being used here the additions are also then done using int16, so it should then work.
ashrafkhatri



Joined: 06 May 2008
Posts: 14

View user's profile Send private message

PostPosted: Mon Feb 27, 2017 3:15 am     Reply with quote

Thanks;
Code is now working
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