Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 7th, 2004, 12:15 PM
Registered User
 
Join Date: Jul 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to extract number from a string correctly?

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 = 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;


Reply With Quote
  #2 (permalink)  
Old March 7th, 2004, 03:52 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

First of all, your statement that "the string is not ended with \0" is incorrect. In C there is no "string" data type. A string constant, defined by something between pairs of " marks has a number of bytes equal to one more than the number of characters between the " marks.

I assume that BYTE is "#define"d as a char.

Then your statement
Code:
BYTE deliminator[] = "PORT 10,247,56,67,9,46\r\n";
defines a char array that has 25 bytes (the 24 chars between the
" marks and an additional byte that is initialized to '\0'.


How do I know this?

Try the following:

Code:
#include <stdio.h>
#include <string.h>
#define BYTE char


int main()
{
  BYTE mystring[] = "PORT 10,247,56,67,9,46\r\n";
  int i;

  printf("\n\n");

  printf("  sizeof(BYTE) = %d\n", sizeof(BYTE));
  printf("  sizeof(mystring) = %d\n", sizeof(mystring));
  printf("  strlen(mystring) = %d\n", strlen(mystring));

  printf("\n\n");
  for (i = 0; i < sizeof(mystring); i++) {
    printf("<%02X> ", mystring[i]);  }
  printf("\n\n");

  return 0;
}
Now as to your program: Your first call to strtok returns a pointer to the string "10", since it has skipped over the "PORT ". Successive calls return pointers to strings "247", "56", etc.

Note that i is equal to 3 when the pointer to "9" is returned, and i is equal to 4 when the pointer to "46" is returned.

Now you want to convert the strings to integers, so you could use sscanf(), atoi(), or other functions to do so.

 The statement
Code:
hi_byte = *str_ptr;
sets hi_byte equal to the ASCII value of the first digit. Not what you want.

I say this, because your function make16() operates on two BYTEs.

By the way, what is an Int8 ? I hope it's not supposed to be an eight-bit integer, since your function must return a 16-bit value.

Dave

Dave
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract part of String carrie09 Access 6 August 24th, 2007 10:04 AM
extract string from value of attribute camacho XSLT 1 August 16th, 2007 02:44 AM
Extract first numeric string blkskullwork Javascript 1 November 6th, 2006 02:37 AM
how to extract from a string fraijo SQL Server 2000 3 October 25th, 2006 09:55 PM
How to extract a substring from a string.... muralish MySQL 2 May 18th, 2005 06:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.