CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

atoi conversion

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ibg



Joined: 19 Nov 2003
Posts: 22

View user's profile Send private message

atoi conversion
PostPosted: Mon May 17, 2004 12:45 pm     Reply with quote

Hi,

I'm parsing a string using the function 'strtok'. I want to assign the different elements I'm getting to integer variables (unsigned int32, unsigned long and unsigned int). The problem is that I don't get the right conversion if the string is greater than '255'. Any idea about how can I fix that?

The code is the following
Code:

#include <18F452.h>      // The device (PIC 18F452) include file
#device *=16   ADC=10

#define RMC_LENGTH 85
#define FISH_LENGTH 25

#include <string.h>
#include <stdlib.h>

#use delay(clock=19660800)   // Sets the speed for the PIC (it�s meausured in cycles per second)

#use fast_io(A)            // Fast method of doing Input/Output
#use fast_io(B)            // The direction register will be set later with the "set_tris_X()" instruction
#use fast_io(C)
#use fast_io(D)

// I/O ports are mapped in memory (Have a look to the Special Function Register (SFR) of PIC18F452)
#byte PORTA  = 0xF80
#byte PORTB  = 0xF81
#byte PORTC  = 0xF82
#byte PORTD  = 0xF83
#byte PORTE  = 0xF84

#use rs232(baud=38400, xmit=PIN_C0, stream=DEBUG)

struct data_fish{
   unsigned int32 frequency;
   unsigned int pulses;
   unsigned long signal_strength;
   unsigned int32 time_unit;   
};

struct data_fish fish_data;
//char rmc_string[RMC_LENGTH]= "GPRMC,124654.189,A,6537.0117,N,02208.2693,E,172.16,210.05,260603,,*35";
char fish_string[FISH_LENGTH]= "FISH,151424,10,150,5";    // Format -> $FISH,FREQUENCY,PULSES,SIGNAL STRENGTH,TIME_UNIT"
char delim[2]= ",";
char *pointer;

//-----------------------------------------------------------------------------------
// Functions used by the main program
void init();

//----------------------------------------------------------------------------------
// Main program
void main(void){

   int counter= 1;

   init();
   fprintf(DEBUG, "Starting 1...\n\r");

   fprintf(DEBUG, "\r\nFISH_STRING= %s \n", fish_string);
   pointer= strtok(fish_string, delim);
     while(pointer != 0)
   {
      if (counter == 2)
      {
          fprintf(DEBUG, "2) %s\n", pointer);
         fish_data.frequency= (unsigned int32)atoi(pointer);
      }
      else if (counter == 3)
      {
          fprintf(DEBUG, "3) %s\n", pointer);
         fish_data.pulses= (unsigned int)atoi(pointer);
      }
      else if (counter == 4)
      {
          fprintf(DEBUG, "4) %s\n", pointer);
         fish_data.signal_strength= (unsigned long)atoi(pointer);
      }
      else if (counter == 5)
      {
          fprintf(DEBUG, "5) %s\n", pointer);
         fish_data.time_unit= (unsigned int32)atoi(pointer);   
      }
      pointer= strtok(0, delim);          
      counter++;
   }
   counter= 0;

   fprintf(DEBUG, "DATA FISH INFO \n");
   fprintf(DEBUG, "%lu\n", fish_data.frequency);
   fprintf(DEBUG, "%d\n", fish_data.pulses);
   fprintf(DEBUG,   "%lu\n", fish_data.signal_strength);
   fprintf(DEBUG,   "%lu\n", fish_data.time_unit);
}

//----------------------------------------------------------------------------------
// Initialisation routine
void init()
{
   // SET_TRIS is necessary when using fast_io (in order to establish the pin direction)
   // Remember:
   //   1 -> in
   //   0 -> out

   set_tris_a(0x00);       // 0000 0000
   set_tris_b(0x00);       // 0000 0000   
   set_tris_c(0x00);         // 0000 0000
   set_tris_d(0x00);       // 0000 0000
}
//----------------------------------------------------------------------------------


Thanks for all,

ibg
Haplo



Joined: 06 Sep 2003
Posts: 659
Location: Sydney, Australia

View user's profile Send private message

PostPosted: Mon May 17, 2004 5:16 pm     Reply with quote

Quoting from CCS manual:

Quote:

Syntax:
ivalue = atoi(string)
or
lvalue = atol(string)
or
i32value = atoi32(string)


Parameters:
string is a pointer to a null terminated string of characters.


Returns:
ivalue is an 8 bit int.
lvalue is a 16 bit int.
i32value is a 32 bit int..




So you need to use atol() or atoi32(), depending on the size of your numbers.
Guest








It's working :-)
PostPosted: Wed May 19, 2004 11:34 am     Reply with quote

Thanks for the help :-) It's working now.

Vi ses,

ibg
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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