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 support@ccsinfo.com

Help extract string

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



Joined: 12 Oct 2009
Posts: 6

View user's profile Send private message

Help extract string
PostPosted: Mon Oct 12, 2009 12:29 pm     Reply with quote

Hello,
I want extract one substring from a string.
Code:
char string [] = "FI000142350       123456789";

I would like to extract a substring such as providing the result "FI0001" or "12345", etc..
then the string "12345" I would like to turn into the number (atof?)
I have see on the forum, but not found info about this.
Can someone help me, I am new in PIC programming, I want use the PIC16F877 with the RS232.

Thanks
Jo
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 12, 2009 1:02 pm     Reply with quote

See this thread on emulating the MID$ function:
http://www.ccsinfo.com/forum/viewtopic.php?t=27121
Jo



Joined: 12 Oct 2009
Posts: 6

View user's profile Send private message

substring from a string.
PostPosted: Mon Oct 12, 2009 4:12 pm     Reply with quote

Thanks PCM programmer,
Sorry but I did not understand how it can be adapted
my request. Sorry but I am new to programming
You may write me, please, a couple of lines?
Jo
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 12, 2009 4:16 pm     Reply with quote

You should at least try to do it.
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Tue Oct 13, 2009 2:24 am     Reply with quote

It looks to me as if you want the strstr() function.

See this thread:
http://www.ccsinfo.com/forum/viewtopic.php?p=60787
Jo



Joined: 12 Oct 2009
Posts: 6

View user's profile Send private message

Help extract string
PostPosted: Wed Oct 14, 2009 3:03 am     Reply with quote

Hello,
Thanks to read this message.
For the extraction of strings I've tried this listing. I managed to extract a substring. I can not turn it into numbers.
Any help ?
Code:

#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
#include <stdlib.h>

char source[10] = "123456789";
char dest[10];
int8 a;
//============================   
void main()
{
memcpy(dest,&source[4],4);
printf(dest);  // Print all OK !!!
a=atoi(dest);  // Try to transform this in number
printf("\r\n");
printf(a);     //    " a "   is empty
while(1);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Oct 14, 2009 1:16 pm     Reply with quote

Several things need to be done to fix it. See the changes shown in
bold below:
Quote:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)
#include <stdlib.h>

char source[10] = "123456789";
char dest[10];
int16 a;
//============================
void main()
{
memcpy(dest,&source[4],4);
dest[4] = 0; // Add string terminator
printf(dest); // Print all OK !!!
a=atol(dest); // Try to transform this in number
printf("\r\n");
printf("%lu", a); //
while(1);
}

1. You're extracting 4 digits, which requires a 16-bit integer to hold.
So 'a' must be declared as an 'int16'.
2. memcpy doesn't automatically add a string terminator byte at
the end of the copied string. You have to add a line of code to do it.
3. You are transforming a 16-bit number, so you must use the atol()
function (ends with a lower case 'l', which stands for 'long').
4. You are displaying a 16-bit unsigned number, so you must use "%lu"
as the printf format string.
Guest








PostPosted: Wed Oct 14, 2009 3:11 pm     Reply with quote

Hello,
Thanks for the reply, very kindly.
Now go to examine your clear note.
Jo
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