Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

parsing a character array

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
rspiege1
Asked:
rspiege1
  • 4
  • 4
1 Solution
 
AxterCommented:
You can use strstr
0
 
AxterCommented:
#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
 
rspiege1Author Commented:
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
rspiege1Author Commented:
Do you know if I could be loosing something then i convert things from a u_char* to a char*?
0
 
AxterCommented:
>>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
 
AxterCommented:
>>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
 
rspiege1Author Commented:
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
 
rspiege1Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now