2015.05.20 Linux Hex_view Source
#include <stdio.h>
#include <pcap/pcap.h>
void Hex_view(void const *,unsigned int);
int main()
{
char * cpNICname;
char caErrMsg[PCAP_ERRBUF_SIZE];
pcap_t * stpDS;
const unsigned char * ucpData;
struct pcap_pkthdr stInfo;
cpNICname=pcap_lookupdev(caErrMsg);
if(0==cpNICname)
{
printf("ErrMsg : [%s]\n",caErrMsg);
return -99;
}
printf("NICName : [%s]\n",cpNICname);
printf("ErrMsg : [%s]\n",caErrMsg);
stpDS=pcap_open_live(cpNICname,1500,1,0,caErrMsg);
ucpData=pcap_next(stpDS,&stInfo);
Hex_view(ucpData,(16*20));
pcap_close(stpDS);
return 0;
}
void Hex_view(void const * vP,unsigned int uiLen)
{
unsigned int uiCnt;
unsigned int uiLine;
printf("============================================================================\n");
printf(" Address Hex ASCII\n");
printf("----------------------------------------------------------------------------\n");
for (uiLine = 0; uiLen > uiLine;uiLine=uiLine+16)
{
printf(" %08X ", vP);
for (uiCnt = 0; uiCnt < 16; ++uiCnt)
{
printf("%02X ", *((unsigned char*)vP));
vP = (char*)vP + 1;
}
vP = (char*)vP - 16;
for (uiCnt = 0; uiCnt < 16; ++uiCnt)
{
if (32 > *((unsigned char*)vP))
{
putchar('.');
}
else if (127 < *((unsigned char*)vP))
{
putchar('.');
}
else
{
printf("%c", *((unsigned char*)vP));
}
vP = (char*)vP + 1;
}
putchar('\n');
}
return;
}