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

Trouble converting parsed string to INT16

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



Joined: 26 Feb 2014
Posts: 24

View user's profile Send private message

Trouble converting parsed string to INT16
PostPosted: Tue Jul 05, 2016 6:02 am     Reply with quote

Hi All

I'm hoping for some comment on trying to find where I have messed this program. This test program accepts serial input and parses out separate strings from comma delimiters. My trouble has appeared when converting the string to int16 - the char array outputs ok as a string but fails as an int16. See following test program and test output:

Code:
#include <18F4620.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlibm.h>

//FUSES
#FUSES NOWDT                      //No Watch Dog Timer
#FUSES INTRC                      //Internal RC Osc, with CLKOUT
#FUSES NOPROTECT                  //Code not protected from reading
#FUSES NOBROWNOUT
#FUSES NOPUT                      //No Power Up Timer
#FUSES NODEBUG                    //No Debug mode for ICD
#FUSES NOLVP                      //No low voltage programming
#FUSES NOWRT                      //Program memory not write protected
#FUSES CCP2B3                     //CCP2 output on RB3 and not RC1
#FUSES MCLR                       //Master Clear pin enabled

#define TX   PIN_C6
#define RX   PIN_C7

#use delay(clock=8000000)
#use RS232 (BAUD = 9600, XMIT = TX, RCV = RX, STREAM = COM_A, ERRORS)

//outputs binary from int16
void printBint16(int16 b){
   int8 i;
   for (i = 15; i > 0; i--) {
      if(i==8) printf(" ");
     
      if(bit_test(b, i-1))
         printf("1");
      else
         printf("0");
   }
}

void main(void) {

   char strCommand[100];
   int8 wordCount=0;
   char *ch;
   char *stpChar="\r\n";
   char *strCmd[12];
   char tok[]=",";
   
   //print ready prompt
   printf("TEST START");
   printf("->");
   
   //receive command
   gets(strCommand);
   
   //remove trialing new line char
   strCommand[strcspn(strCommand, stpChar)] = 0;
   
   //split in string into seperate vars
   ch = strtok(strCommand, tok);
   strCmd[wordCount]=(char*)malloc(strlen(ch) + 1);
   strcpy(strCmd[wordCount],ch);
   
   while (ch != NULL) {
      wordCount++;
      //printf("%s ", ch);
      ch = strtok(NULL, tok);
      strCmd[wordCount]=malloc(strlen(ch) + 1);
      strcpy(strCmd[wordCount],ch);
   }
   switch(strCmd[0]) {
      case "s":
         //check if 2nd value parsed
         if(wordCount>=1){
            int16 atoi_val;
           
            printf("string val= %s\r\n",strCmd[1]);
           
            printf("size_of=%d\r\n",sizeof(strCmd[1]));

            printf("binary of strCmd[1]=");
            printBint16(strCmd[1]);
            printf("\r\n");
           
            atoi_val=atoi(strCmd[1]);
            printf("atoi_val=%lu\r\n",atoi_val);
           
            printf("binary of atoi_val=");
            printBint16(atoi_val);
            printf("\r\n");
         }
         else printf("s error");
         break;
 
      default:
         printf("unknown command\r\n");               
   }

   free(strCmd);
   while(1);
} //end main


Code:
input "s,512"[cr]
TEST START->string val= 512
string val= 512
size_of=2
binary of strCmd[1]=0000000 10111001
atoi_val=0
binary of atoi_val=0000000 00000000

input "s,1023"[cr]
TEST START->string val= 1023
size_of=2                                                         
binary of strCmd[1]=0000000 10111011
atoi_val=255
binary of atoi_val=0000000 11111111

_________________
I type therefore I press buttons
alyeomans



Joined: 26 Feb 2014
Posts: 24

View user's profile Send private message

PostPosted: Tue Jul 05, 2016 6:25 am     Reply with quote

Solved. atoi() is for 8 bit and atol() for long integers - works ok for int16 not sure of higher.

Al
_________________
I type therefore I press buttons
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Tue Jul 05, 2016 8:21 am     Reply with quote

If you look in stdlib, you will find the code for atol. Immediately followed by the code for atoi32. Solves the 32bit problem. Smile

There is also code there for PCD only, giving atoi48 and atoi64.
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