Solved

parsing a character array

Posted on 2006-06-15
8
245 Views
Last Modified: 2010-04-01
I have the following object:

const u_char* pData;


Inside this object is a bunch of junk. "adfasdfasdf11=asdfasdf"  What is the easiest way to find where the 11= begins in this object.

thanks
0
Comment
Question by:rspiege1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 16914663
You can use strstr
0
 
LVL 30

Accepted Solution

by:
Axter earned 125 total points
ID: 16914702
#include <string.h>

const u_char* pData = "adfasdfasdf11=asdfasdf";
const char* srchStr = "11";
char *pdest = strstr( pData, srchStr);

if ( pdest != NULL )
{
  int result = (int)(pdest - pData + 1);
  printf( "%s found at position %d\n", srchStr, result );

}
0
 

Author Comment

by:rspiege1
ID: 16914923
It seems to work.  I had to add (Char*) casting for pData in two lines above to get it to compile.  The only thing is it does not return an integer.  instead i get a bunch of garbage.  It may be what I have stored in pData.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rspiege1
ID: 16914966
Do you know if I could be loosing something then i convert things from a u_char* to a char*?
0
 
LVL 30

Expert Comment

by:Axter
ID: 16914976
>>The only thing is it does not return an integer.

It returns a pointer to the position where the string starts.
You have to use pointer arithmitic to get the index int value, as in above example:
int result = (int)(pdest - pData + 1); //Pointer arithmetic
0
 
LVL 30

Expert Comment

by:Axter
ID: 16914995
>>Do you know if I could be loosing something then i convert things from a u_char* to a char*?

I don't think that's the problem.

Try posting your new code.
0
 

Author Comment

by:rspiege1
ID: 16915152
you solved what I was looking for.  thank you for the help.  My problem is that pData is being filled while using the WinPcap library and it is the way it is reading things.  I'll figure that piece out.

thanks,
Rob
0
 

Author Comment

by:rspiege1
ID: 16915207
actually one more question.  is there a function in c++ to search for a string inside a string.

string temp = "hello world";

how could i search to see if world is in this string?

thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question