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

[rs-232] string to long int conversion

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



Joined: 11 May 2009
Posts: 22

View user's profile Send private message

[rs-232] string to long int conversion
PostPosted: Fri Oct 09, 2009 7:57 am     Reply with quote

Hi, I've got a pic connected to a PC via rs232. The pc send me a number (i.e. 2010) like 4 character (char 2, char 0, char 1, char 0), so the pc send me the ascii code for a single digit.
How can I translate this series of character into a unique long int representig the 2010 number?

thanks to anyone can help me.
Audi80



Joined: 07 Sep 2007
Posts: 41

View user's profile Send private message

PostPosted: Fri Oct 09, 2009 8:16 am     Reply with quote

You could do something like this:

Code:

int32 convert(char * ptr){
   int32 aux = 0;
   char value[5] = "";
   memset(value,0,sizeof(value));
   value[0] = ptr[0];
   value[1] = ptr[1];
   value[2] = ptr[2];
   value[3] = ptr[3];
   printf("Received value: %lu \r\n",atoi32(value));
   return atoi32(value);
}


Hope it helps...
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Fri Oct 09, 2009 8:39 am     Reply with quote

It all depends on some things.
What is the maximum value you will receive ?
Are the strings null terminated ?
Are any other characters sent with the number ?

If you receive the chars into an array and terminate it with a null char when you receieve the last one you can use

atoi(string) if it is max 8 bit value
atol(string) it is max a 16 bit value
atoi32(string) if it is max a 32 bit value.

If you are the one receving the chars you can do the math as you receieve each char

Code:

int32 val;
int c;

// get number routine
val = 0;
do
{
   c = getc();
   val = (val * 10) + (c - '0');
} while (c != 0);
 


OK, so I just noticed you want a long int, it still depends on how you process the received chars.
Torello



Joined: 29 Sep 2006
Posts: 120

View user's profile Send private message

PostPosted: Thu Oct 15, 2009 11:48 am     Reply with quote

Hi Blo,

I've written this function to do all that for an int16. Maybe you can expand to i32? Personally I dislike atoi32.
If you need examples how to use, I'l ppost them tomorrow. (need to go now)

Regards,
Edwin
Code:

//==============================================================================
//-UNSIGNED- decimal conversion
//Converts a string: variable length: ends with a non-decimal <=5 chars (len=0),
//                   or user specified length (len=1..5)
//returns error(=0) if:
// - len=0 and -no- seperator found after >5 digits (value too big)
// - value>65535
// - Len<>0 and length is not met (too few digits), -but- more is OK! (section conversion)
//if error -> result stays untouched

//if NO error -AND- len=0; function returns:
// - 1: conversion OK and string ended with '\0')
// - >1: conversion OK and string ended with a separator (so maybe more numbers to convert)
//       returns length -including seperator-! (assuming one char!)

//if NO error -AND- len=x; function returns:
// - 1: conversion OK:

int8 StrToi16DecU(char *s, int8 len, int16 *result) {
  char dig;
  int8 i;
  int16 sum=0;
 
  if (len>5) len=5;                                     // f*up proof
  for(i=1; i<=6; i++) {                                 //max loop for 6 chars (max 5 digits + end)
    dig=s[i-1];                                         //get the char; correct for i starts at 1
    if ( (dig<48) || (dig>57) ) {                       //T: non-decimal; end conversion
      i--;                                              //correct for length
      break;                                     
    } 
    if (i==6) goto sti16d_err_exit;                     //..5 digits done, 6th is also a digit -> number too big.
   
    dig -=48;                                           //undo ASCII offset     
    if (i==5) {                                         //T: 5th digit; start boumdary checking
      if (sum>6553) goto sti16d_err_exit;               //T: sum already too big for mult by 10
      if ((sum==6553) && (dig>5)) goto sti16d_err_exit; //T: mult by w10 ould do, but last digit too big
    }     
    sum = (sum*10) + dig;
    if (len==i) break;                                  //T: specified length match; stop calculation;
  }
  if (!i)      goto sti16d_err_exit;                    //no single digit found (reason to start i at 1) 
  if ((i<len)) goto sti16d_err_exit;                    //specified len does not match
 
  *result = sum;
  if (!dig) return(1);                                  //string ends '/0'
  else {
    if (!len) return(++i);                              //returns length including separator
    return (1);                                         //return Ok for specified len
  }
  sti16d_err_exit:
  return(0);   
}
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