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;
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.
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