|
|
View previous topic :: View next topic |
Author |
Message |
matheuslps
Joined: 29 Sep 2010 Posts: 73 Location: Brazil
|
Why atol do not work with int16 array? |
Posted: Fri Jan 21, 2011 5:29 pm |
|
|
Just a doubt that I have about the function ATOL(array).
I did a simple example here to test the function:
Code: | #include <16F877A.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES XT //Crystal osc <= 4mhz
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES BROWNOUT //Reset when brownout detected
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include <stdlib.h>
char vetor[7]=
{
0x30,
0x33,
0x36,
0x38,
0x36,
0x37,
0x00
};
int16 x;
void main()
{
while (TRUE)
{
x = atol(vetor);
printf ("x=%lu\n\r",x);
delay_ms (500);
}
} |
If i declare vetor like int8 or char, the code works. But if I tell the compiler that is an int16, it do not work. Why?
Compiler 4.057.
Thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jan 21, 2011 5:46 pm |
|
|
The atol() function works with an array of bytes (int8) that holds a string.
It does not work with an array of 'int16' values. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Jan 21, 2011 6:34 pm |
|
|
I am not sure how I would want it to work with a 16 bit input. Should it do two conversions for high and low byte, or should it ignore the high byte?
I suggest you write your own function that does what you want it to. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Sat Jan 22, 2011 3:08 am |
|
|
Others have given both parts of the answer
Think about it. What does 'A to L' stand for?.
ASCII to long.
AtoL, takes ASCII characters (bytes....), and decodes this text to a 'long' integer.
Why would you want to waste space storing ASCII 8bit text, in a 16bit array?.
If you have numbers presented this way, two choices:
1) Move them to a character array before decoding.
2) Write your own function.
If you have 16bit values, they are not going to be 'ASCII', so 'Atol, is almost certainly the wrong function.
If they are just numbers, then no ASCII conversion is needed.
If they are ASCII coded, then ask yourself again, 'why am I wasting half the RAM storing nothing', and store them into a character array instead.
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
|