|
|
View previous topic :: View next topic |
Author |
Message |
info@ckmintech.com
Joined: 16 May 2006 Posts: 39
|
RS232 printf |
Posted: Wed May 17, 2006 6:50 am |
|
|
I have problem to send a long integer or float number to a PC through RS232
printf("Output=%ld",123456);
The data received by the Hyperterminal is
Output=-5046
It's fine to send unsigned integer ans string.
Thanks |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
Re: RS232 printf |
Posted: Wed May 17, 2006 7:13 am |
|
|
info@ckmintech.com wrote: | I have problem to send a long integer or float number to a PC through RS232
printf("Output=%ld",123456);
The data received by the Hyperterminal is
Output=-5046
It's fine to send unsigned integer ans string.
Thanks |
123456L perhaps? _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
Ttelmah Guest
|
|
Posted: Wed May 17, 2006 8:02 am |
|
|
The problem here, is that the 'L' specifier, has two meanings, according to the 'type' of the value being received. So if you give a 16bit value to it, it uses 16bit arithmetic, while if you give it a 32bit value, it switches 'up' again, and uses 32bit arthmetic. Unfortunately, using a constant (even though too large to fit into an int16), sometimes only results in the int16 behaviour, and you therefore overflow.
Now I am assuming that the code given is only a demo, otherwise there is no point at all in using such a constant inside a printf. What you need to do, is to ensure that the value received by the printf routine, is stored in a signed int32 variable, or cast into such a type.
You should find that:
printf("Output=%ld",(signed int32)(123456));
If you are using a variable to 'feed'the routine, you need to verify that these are signed int32 values, and also whn perfoming earlier arithmetic, be very careful about the type casting. For instance:
Code: |
int16 val1, val2;
signed int32 result;
val1=100;
val2=60000;
result=val1*val2;
|
Will give the _wrong_ answer, since in the actual 'sum', the parts are all int16. You will get (60000*100)%65536....
When doing all such sums, which are not explicitly using the larger data types, you need to ensure that you 'cast' the types up to the form that you want to do the arithmetic in (the '(signed int32)' declaration is a 'cast'.
Best Wishes |
|
|
|
|
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
|