Hi,
can any one please tell me how to convert data type long to an integer in Arduino. Quick response is appreciated
Regards,
Hema
Hi,
can any one please tell me how to convert data type long to an integer in Arduino. Quick response is appreciated
Regards,
Hema
A long is an integer.
No conversion necessary
In my case i am actually trying to compare a long value with an integer but i am not getting accurate results. can any one pl suggest some solution for it.
HemaChowdary:
In my case i am actually trying to compare a long value with an integer but i am not getting accurate results. can any one pl suggest some solution for it.
Show us your code and a "non accurate result" and we may be able to help you out.
Groove:
A long is an integer.
No conversion necessary
A integer takes 2 bytes and a long takes 4 bytes on Arduino.
Best regards
Jantje
There is a concept of casting in c programming laungage and its succedors like c++ arduino ide etc
int number=0;
long secondNumber=1234567;
If(secondNumber==(long)number)
{
True Stuff goes here
}
Else{
false stuff here
}
Morenlight on the casting stuff
its always better to cast the smaller one(with respect to memory , in our case in is smaller than long) to be casted into bigger on
Casting a variable wont change its value , it will just consider the number as of a different data type
example :
Int number =0 ;
Float secondNumber = number ; // THIS WILL GIVE ERROR
float secondNumber = (float) number ; //This will work like charm
n I hope this works too (I dun have arduino yet with me I have ordered it so idk much but I hope the following example works too:
Void setup(){
Int number = 123 ;
Serial.begin(9600);
}
Void loop()
{
Serial.println((float) number);
}
This will print "1234.000000" since it is cased to float data type , which has 6 digits of precision
Grumpy_Mike:
int() - Arduino Reference
ya I guess this ia how u cast in arduino ..
so I will rewrite my example above:
int number =0 ;
Long secondNumber=123456789;
Long convertedNumber = long(number); // the first variable number is stored in convertedNumber as a long variable..
u really got to do trial and error