janet
Joined: 17 Feb 2004 Posts: 23
|
How to use extract number from a sentence correctly? |
Posted: Sun Mar 07, 2004 8:17 am |
|
|
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; |
|