Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

parsing a character array

Posted on 2006-06-15
8
Medium Priority
?
247 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 500 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

660 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