c++visual-c++visual-studio-2013pcapwinpcap

Use Winpcap to read saved file(.PCAP) in uint16 structure


I used Winpcap to read saved .pcap file with code below but the output put in unsigned char "u_char data" in hex format. How can I read .pcap file in uint16 structure in Decimal format?

string file = Address_Data;
char errbuff[PCAP_ERRBUF_SIZE];
pcap_t * pcap = pcap_open_offline(file.c_str(), errbuff);
struct pcap_pkthdr *header;
typedef unsigned char   u_char;
const u_char *data;
u_int packetCount = 0;

while (int returnValue = pcap_next_ex(pcap, &header, &data) >= 0)
{
    printf("Packet # %i\n", ++packetCount);
    printf("Packet size: %d bytes\n", header->len);
    if (header->len != header->caplen)
        printf("Warning! Capture size different than packet size: %ld bytes\n", header->len);
    printf("Epoch Time: %d:%d seconds\n", header->ts.tv_sec, header->ts.tv_usec);
    for (u_int i=0; (i < header->caplen ) ; i++)
    {
        if ( (i % 16) == 0) printf("\n");
        printf("%.2x ", data[i]);
    }
   printf("\n\n");
}

For example data[0]=24 , data[1]=8a I want data format will be uint16 and Decimal not hex, like: data[0]= 9354 [248A hex]


Solution

  • I resolved my problem with easy converting of two 8 bit to one 16 bit like below , because I did not see any reading format in winpcap.

    uint16_T(data[2 * i]) * 256 + uint16_T(data[(2 * i) + 1]