Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have an unsigned char *data that gives me the output of the following....

CSS
-------------IP Data Begins-------------
HTTP/1.1 200 OKP3P: policyref="http://g
oogleads.g.doubleclick.net/pagead/gcn_p3p_.xml"
, CP="CURa ADMa DEVa TAIo PSAo PSDo OUR IND UNI
PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"
Content-Type: text/html; charset=UTF-8
X-Content-Type-Options: nosniffContent-
Encoding: gipDate: Mon, 29 Aug 201
1 21:19:36 GMTServer: cafeCache
-Control: privateContent-Length: 4863
X-XSS-Protection: 1; mode=block


This code isn't returning the correct output...it's always 0. I'm sure it has something to do with the unsigned char *data.

C++
int mycount;
char myints[] = {reinterpret_cast<char>(data)};
mycount = (int) count (myints, reinterpret_cast<char>(data), 'HTTP');
cout << "HTTP Appears " << mycount << " times.\n";
return;</char></char>
Posted
Comments
Chuck O'Toole 7-Sep-11 18:55pm    
Post the count() function as it's the thing returning 0.
Member 7766180 7-Sep-11 19:39pm    
This is the entire code.
Chuck O'Toole 7-Sep-11 18:57pm    
I see you posted it in another question. Please post complete questions, don't expect us to piece together the information from multiple questions.
Member 7766180 7-Sep-11 19:40pm    
This is the only place as I just wrote the code.
Chuck O'Toole 7-Sep-11 19:44pm    
I asked you to post the "count()" function as it isn't in this post at all. You say this is all the code. Then, in another question (http://www.codeproject.com/Questions/251201/String-Search-A-Better-Way) you post a "count()" function. It that you or someone posing as you?

1 solution

Your count function is probably wrong. Try something like this instead (Warning, uncompiled code ahead!):

C++
/// returns the number of times element appears in str.
int count( const std::string& str, const char* element )
{
    int count = 0;
    for( size_t last = -1; 
         ( last = str.find( element, last + 1 ) ) != std::string::npos; 
         ++count );
    return count;
}

int mycount = count( data, "HTTP" );


Take a look at the standard strings library[^].
 
Share this answer
 
v3
Comments
Member 7766180 7-Sep-11 23:29pm    
I think the problem is with "data" It is clearly returning text, but when I go to run it from the last piece of code to count, it doesn't seem to be returning anything. Is it the unsigned char problem? Here is the preceeding code that does return value.

void printRawData(unsigned char *data, int length, int more)
{

int i, c=0;
printf(" -------------IP Data Begins-------------\n");
for (i=0; i<length; i++)
="" {
="" if="" ((data[i]="">30 && data[i]<122) ||
(((data[i]==10) || (data[i]==13) || (data[i]==123) || (data[i]==125))
&& (more>0)))
{
printf("%c", data[i]);
c+=1;
}
else
{
//printf("[%i]", data[i]);
c+=3;
if (data[i]>9) c++;
if (data[i]>99) c++;
}
if (c>=47)
{
printf("\n");
c=0;
}
}
}

#include "tcppacket.h"
#include "udppacket.h"
//#include "content.h"

void printIpPacket(unsigned char *data, int length, int more)
{
printf("-----------------Packet Begins-----------------\n");
printf("IP Version: %i \nIP Packet Size: %ibytes \nIP Id: %i\n",
(data[0]>>4), (data[2]*256)+data[3], (data[4]*256)+data[5]);

printf("IP Fragment: %i \nIP TTL: %i \nIP HL: %iwds \nIP Protocol: %i\n",
((int)(data[6]>>4)*256)+data[7], data[8], ((char)(data[0]<<4))>>4, data[9]);

printf("IP Source: %i.%i.%i.%i \nIP Destination: %i.%i.%i.%i\n",
data[12], data[13], data[14], data[15],
data[16], data[17], data[18], data[19]);

if (data[9]==6)
printTcpPacket(data+(((char)(data[0]<<4))>>2), length-(((char)(data[0]<<4))>>2), more);
else if (data[9]==17)
printUdpPacket(data+(((char)(data[0]<<4))>>2), length-(((char)(data[0]<<4))>>2), more);
else
printRawData(data+(((char)(data[0]<<4))>>2), length-(((char)(data[0]<<4))>>2), more);
Paul Heil 8-Sep-11 9:36am    
Now I'm confused. In your post, you said the contents of 'data' were a given. Reading between the lines, I guessed you were trying to print the number of times the string "HTTP" appears in the zero-terminated character string 'data'. The value returned by your `count()` function isn't what you expected. (it returned 0, but should return 1).

If this synopsis is correct, then the issue must be in your `count()` function. Try replacing it with the one I posted.

If this is incorrect, please restate your problem.

If you think the `data` variable doesn't contain the information you think it does, try running your code on the debugger and watching the `data` variable to see what it actually does contain.
Member 7766180 9-Sep-11 13:25pm    
Thank you Paul. The statement in your first paragraph is correct. It should return a 1. But I keep getting 0. I'm thinking that it's not reading "data". I'll try debugging again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900