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

How to use extract number from a sentence correctly?

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



Joined: 17 Feb 2004
Posts: 23

View user's profile Send private message

How to use extract number from a sentence correctly?
PostPosted: Sun Mar 07, 2004 8:17 am     Reply with quote

hi,

I wish to extract 9 and 46 from a string
"PORT 10,247,56,67,9,46\r\n"
, where \r is 0x0D and \n 0x0A

Note that the string is not ended with \0, so the string length is 24 bytes.
Note that also the number of bytes for each number is not fixed,
for example, "PORT 101,247,56,67,9,46\r\n" could be 25 bytes in length.

However, the maximum of each number is 255, which is FF in hex.

the 9 and 46 is a port number, which is 2350.
9 = 0x09, 46 = 0x2E;

2350 == 0x09 2E

In short, the function will return 0x092E.
I use str

to ease you to test the code, I paste the code i typed here,
This function will not work because *string_ptr only returns one byte.

int8 FTPGetRemotePort(BYTE *buffer) {
BYTE *string_ptr;
BYTE deliminator[] = "PORT ,\r\n";
BYTE i;
BYTE high_byte;
BYTE low_byte;

string_ptr = strtok(buffer, deliminator);

for (i = 0; i < 6; i++) {
string_ptr = strtok(NULL, deliminator);

if (i == 4) {
high_byte = *string_ptr;
}

if (i == 5) {
low_byte = *string_ptr;
}
}
return make16(high_byte, low_byte);
}
BYTE buffer[40];

buffer[0] = 0x50;
buffer[1] = 0x4f;
buffer[2] = 0x52;
buffer[3] = 0x54;
buffer[4] = 0x20;
buffer[5] = 0x31;
buffer[6] = 0x30;
buffer[7] = 0x2c;
buffer[8] = 0x32;
buffer[9] = 0x34;
buffer[10] = 0x37;
buffer[11] = 0x2c;
buffer[12] = 0x35;
buffer[13] = 0x36;
buffer[14] = 0x2c;
buffer[15] = 0x36;
buffer[16] = 0x31;
buffer[17] = 0x2c;
buffer[18] = 0x39;
buffer[19] = 0x2c;
buffer[20] = 0x34;
buffer[21] = 0x36;
buffer[22] = 0x0d;
buffer[23] = 0x0a;
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 07, 2004 4:58 pm     Reply with quote

I once posted some code to parse a comma delimited string
from a GPS message. It may help you.
http://www.ccsinfo.com/forum/viewtopic.php?p=4473#4473
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