Hi friends,
I´m with a simple (but painfull) problem using long variable. I need to transmit serially a 7-digit long variable (signed). I´m transmitting in ASCII mode. So, i pick digit by digit and add 0x30 and send. But the most significant digits are not correct, when the number becomes negative. Here my code. Any suggestion? tanks a lot
unsigned long charMod1Milhao;
unsigned long charMod1MilhaoMod100Mil;
unsigned long charMod1MilhaoMod100MilMod10Mil;
unsigned long charMod1MilhaoMod100MilMod10MilModMil;
unsigned long charMod1MilhaoMod100MilMod10MilModMilMod100;
As far as i know there seems to be a logical error which is rectified in the below code.(I have not checked the below code using a C ). check if it works for u
unsigned long charMod1Milhao;
unsigned long charMod1MilhaoMod100Mil;
unsigned long charMod1MilhaoMod100MilMod10Mil;
unsigned long charMod1MilhaoMod100MilMod10MilModMil;
unsigned long charMod1MilhaoMod100MilMod10MilModMilMod100;
unsigned long charMod1MilhaoMod100MilMod10MilModMilMod100Mil10; //new
charMod1Milhao = (unsigned long)(CotaEixoZ % 10000000); // An extra Zero added
charMod1MilhaoMod100Mil = charMod1Milhao % 1000000; // An extra Zero added
charMod1MilhaoMod100MilMod10Mil = charMod1MilhaoMod100Mil % 100000; // An extra Zero added
charMod1MilhaoMod100MilMod10MilModMil = charMod1MilhaoMod100MilMod10Mil % 10000; // An Extra Zero added
charMod1MilhaoMod100MilMod10MilModMilMod100 = charMod1MilhaoMod100MilMod10MilModMil % 1000; // An extra Zero Added
charMod1MilhaoMod100MilMod10MilModMilMod100Mil10 = charMod1MilhaoMod100MilMod10MilModMilMod100 % 100 //New
Thanks for your suggestion. It made me to see another way to do that, but the problem still remains. The wrong value occurs when the number is negative. When positive is OK. Instead of going +0000.000 to -0000.001, the value goes to a value around -0097.xxx, where 'x' is a random number. I cheked the spell many times. What else do you think can be? I also think must be a sign problem...I don´t know, i´ll try something here. Thanks again
What type of variable is CotaEixoZ? Is that the one you mean by "the long variable is 8 bytes unsigned"?
If it's unsigned, how can it ever be negative?
A 64-bit (8 byte) variable seems very big for storing only a 7-digit value.
If you have sprintf() available (it's a large function), you could use it to easily convert the value into an ASCII string.