View previous topic :: View next topic |
Author |
Message |
Jo
Joined: 12 Oct 2009 Posts: 6
|
Help extract string |
Posted: Mon Oct 12, 2009 12:29 pm |
|
|
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
|
|
|
Jo
Joined: 12 Oct 2009 Posts: 6
|
substring from a string. |
Posted: Mon Oct 12, 2009 4:12 pm |
|
|
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
|
|
Posted: Mon Oct 12, 2009 4:16 pm |
|
|
You should at least try to do it. |
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
|
Jo
Joined: 12 Oct 2009 Posts: 6
|
Help extract string |
Posted: Wed Oct 14, 2009 3:03 am |
|
|
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
|
|
Posted: Wed Oct 14, 2009 1:16 pm |
|
|
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
|
|
Posted: Wed Oct 14, 2009 3:11 pm |
|
|
Hello,
Thanks for the reply, very kindly.
Now go to examine your clear note.
Jo |
|
|
|