Solved

parsing a character array

Posted on 2006-06-15
8
241 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
How to copy an image file into clipboard C/C++? 1 152
Printing the elements of a set declared inside a map in C++. 3 38
Header of docx file 17 97
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now