View previous topic :: View next topic |
Author |
Message |
williankleber
Joined: 17 Sep 2012 Posts: 4 Location: Brasil
|
itol("100000") = 34464...... Why? |
Posted: Mon Sep 17, 2012 2:21 pm |
|
|
Code: | int16 k;
char u[] = "100000";
k = atol(u); |
At the end of this code, the variable "K" takes the value 34464.
I'm using PICC PCW and the PIC is 18F4520.
I will use this algorithm to convert one string with binary sequence, in a binary type.
I thank all for the attention |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Sep 17, 2012 2:25 pm |
|
|
What's the largest number an int16 can hold?
and answer that question for unsigned and signed.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
williankleber
Joined: 17 Sep 2012 Posts: 4 Location: Brasil
|
|
Posted: Mon Sep 17, 2012 2:34 pm |
|
|
I changed my code to
Code: | while (1){
unsigned int32 k;
char u[] = "100000";
printf(lcd_putc,"\fu = %s",u);
delay_ms(2000);
k = atol(u);
printf(lcd_putc,"\fk = %lu",k);
delay_ms(2000); |
and the problem continues,
The number 100000 should fit into a INT32 variable. Does this has to do with processing of 16bit PIC? |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Sep 17, 2012 2:41 pm |
|
|
williankleber wrote: | I changed my code to
Code: | while (1){
unsigned int32 k;
char u[] = "100000";
printf(lcd_putc,"\fu = %s",u);
delay_ms(2000);
k = atol(u);
printf(lcd_putc,"\fk = %lu",k);
delay_ms(2000); |
and the problem continues,
The number 100000 should fit into a INT32 variable. Does this has to do with processing of 16bit PIC? |
The PIC18's are 8bit CPUs (just so you know) and being 16 or 32 bits doesn't matter as the compiler does the work for that. I do 16/32bit math all the time on PIC18's.
So at this point, you should post a small compilable example (and your compiler version) as the second sticky-post to this forum describes.
Also, you're not using Proteus, are you?
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Sep 17, 2012 2:53 pm |
|
|
Better yet -- look at the CCS manual (.CHM) again for atol
And look closely at the other items listed on the same help page.
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
williankleber
Joined: 17 Sep 2012 Posts: 4 Location: Brasil
|
|
Posted: Mon Sep 17, 2012 2:56 pm |
|
|
This is my code:
Code: | #include <18F4520.h>
#use delay(clock=4000000)
#include <stdio.h>
#include <stdlib.h>
#include "lcd.c"
#include <math.h>
void main(){
lcd_init();
while (1){
unsigned int32 k;
char u[] = "100000";
printf(lcd_putc,"\fu = %s",u);
delay_ms(2000);
k = atol(u);
printf(lcd_putc,"\fk = %lu",k);
delay_ms(2000);
}
} |
My compiler version is 4.074
And really, I'm using proteus for simulation. Does that is causing the problem?
I'll mount the circuit on a board, soon put the result.
Thank you very much bkamen, is helping me a lot... |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Sep 17, 2012 3:10 pm |
|
|
williankleber wrote: | This is my code:
My compiler version is 4.074
And really, I'm using proteus for simulation. Does that is causing the problem?
|
In this case, Proteus is not the problem. Look at the CCS PICC compiler manual again.
Just keep in mind that in the future -- Proteus can be a problem in general for emulating PIC code. There's enough features in a PIC that proteus incorrectly emulates nullifying using proteus at all.
Cheers,
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Sep 17, 2012 3:11 pm |
|
|
You still didn't change your code to use atoi32()... |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Mon Sep 17, 2012 5:13 pm |
|
|
FvM wrote: |
You still didn't change your code to use atoi32()... |
Boooo! I told him that! (via "look at the manual") hahaha...
_________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
williankleber
Joined: 17 Sep 2012 Posts: 4 Location: Brasil
|
Fixed... |
Posted: Tue Sep 18, 2012 7:57 am |
|
|
solved using atoi32()...
Follow the hint of bkamen, and found all the documentation in the HELP Files of PICC.
Quote: | Posted: Mon Sep 17, 2012 6:11 pm Post subject:
You still didn't change your code to use atoi32()... |
Sorry FvM and bkamen, I posted the same instant that the bkamen, for this reason the code had not yet been changed...
anyway, thanks to all for help. |
|
|
|