Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将数组读入字符串的最快方法_C_Arrays - Fatal编程技术网

C 将数组读入字符串的最快方法

C 将数组读入字符串的最快方法,c,arrays,C,Arrays,我有一个*p,代表下面的数组,这就是我现在读取它的方式。我需要什么最快的方法来读取它并在C中输入字符串变量。有什么建议吗 char *hexValue=""; while(p) { printf("\n\nstr: %s\n", p); asprintf(&hexValue,"%s%02x",hexValue,p); p++; } 下面是整个功能代码 void dummyProcesssPacket(const struct pfring_pkthdr *h,

我有一个*p,代表下面的数组,这就是我现在读取它的方式。我需要什么最快的方法来读取它并在C中输入字符串变量。有什么建议吗

char *hexValue="";
while(p) {
    printf("\n\nstr: %s\n", p);
    asprintf(&hexValue,"%s%02x",hexValue,p);
    p++;
}
下面是整个功能代码

void dummyProcesssPacket(const struct pfring_pkthdr *h, 
             const u_char *p, const u_char *user_bytes) {
  long threadId = (long)user_bytes;
  printf("\nIN Dummy\n");
  numPkts[threadId]++, numBytes[threadId] += h->len+24 /* 8 Preamble + 4 CRC + 12 IFG */;

#ifdef ENABLE_BPF
  if (userspace_bpf && bpf_filter(filter.bf_insns, p, h->caplen, h->len) == 0)
    return; /* rejected */

  numPktsFiltered[threadId]++;
#endif

  if(touch_payload) {
    volatile int __attribute__ ((unused)) i;

    i = p[12] + p[13];
  }

  if(verbose) {
    printf("\nverbose :");
    struct ether_header *ehdr;
    char buf1[32], buf2[32];
    struct ip *ip;
    int s;
    uint usec;
    uint nsec=0;


    int i=0;
    int j=0,line=0,packSize=h->caplen;
    char *hexValue="";
    while(j<packSize/16)
    {
      printf("%06x: ",line++);
        for(i=0;i<16;i++)
        {
          printf("%02x ",p[j*16+i]);
          asprintf(&hexValue,"%s%02x",hexValue,p[j*16+i]);
        }
        printf("   |");
        /*for(i=0;i<16;i++)
        {
           if(isprint(p[j*16+i]))
             printf("%c",p[j*16+i]);
           else
             printf(".");
        }*/
        printf("|\n");
        j++;
    }
    printf("\nTotal hex value is %s",hexValue);




    if(h->ts.tv_sec == 0) {
      memset((void*)&h->extended_hdr.parsed_pkt, 0, sizeof(struct pkt_parsing_info));
      pfring_parse_pkt((u_char*)p, (struct pfring_pkthdr*)h, 5, 1, 1);
    }

    s = (h->ts.tv_sec + thiszone) % 86400;

    if(h->extended_hdr.timestamp_ns) {
      if (pd->dna.dna_dev.mem_info.device_model != intel_igb_82580 /* other than intel_igb_82580 */)
        s = ((h->extended_hdr.timestamp_ns / 1000000000) + thiszone) % 86400;
      /* "else" intel_igb_82580 has 40 bit ts, using gettimeofday seconds:
       * be careful with drifts mixing sys time and hw timestamp */
      usec = (h->extended_hdr.timestamp_ns / 1000) % 1000000;
      nsec = h->extended_hdr.timestamp_ns % 1000;
    } else {
      usec = h->ts.tv_usec;
    }

    printf("%02d:%02d:%02d.%06u%03u ",
       s / 3600, (s % 3600) / 60, s % 60,
       usec, nsec);

    ehdr = (struct ether_header *) p;
    printf("\n\nBefore Extexted :%d",use_extended_pkt_header);
    if(use_extended_pkt_header) {
      printf("\nafter USE EXE");
      printf("%s[if_index=%d]",
        h->extended_hdr.rx_direction ? "[RX]" : "[TX]",
        h->extended_hdr.if_index);

      printf("[%s -> %s] ",
         etheraddr_string(h->extended_hdr.parsed_pkt.smac, buf1),
         etheraddr_string(h->extended_hdr.parsed_pkt.dmac, buf2));    

      if(h->extended_hdr.parsed_pkt.offset.vlan_offset)
    printf("[vlan %u] ", h->extended_hdr.parsed_pkt.vlan_id);

      if (h->extended_hdr.parsed_pkt.eth_type == 0x0800 /* IPv4*/ || h->extended_hdr.parsed_pkt.eth_type == 0x86DD /* IPv6*/) {

        if(h->extended_hdr.parsed_pkt.eth_type == 0x0800 /* IPv4*/ ) {
      printf("[IPv4][%s:%d ", intoa(h->extended_hdr.parsed_pkt.ipv4_src), h->extended_hdr.parsed_pkt.l4_src_port);
      printf("-> %s:%d] ", intoa(h->extended_hdr.parsed_pkt.ipv4_dst), h->extended_hdr.parsed_pkt.l4_dst_port);




                    char sql_lite[1500];
                    int lastID = mysql_insert_id(conn);
                    printf("\n\n\nLAT IS %d",lastID);
                    char *hexValue="";
                    while(p) {
                  printf("\n\nstr: %s\n", p);
                  asprintf(&hexValue,"%s%02x",hexValue,p);
                  p++;
               }
                    //printf("\n\n char size : p size %lu ",sizeof(p));
                    sprintf(sql_lite, "insert into tblPL1 values ('%d','%s','%s')",'2013-05-11 20:20:20',p[0]);
              puts(sql_lite);
              //error = sqlite3_exec(conn, sql_lite, 0, 0, 0);




        } else {
          printf("[IPv6][%s:%d ",    in6toa(h->extended_hdr.parsed_pkt.ipv6_src), h->extended_hdr.parsed_pkt.l4_src_port);
          printf("-> %s:%d] ", in6toa(h->extended_hdr.parsed_pkt.ipv6_dst), h->extended_hdr.parsed_pkt.l4_dst_port);
        }

    printf("[l3_proto=%s]", proto2str(h->extended_hdr.parsed_pkt.l3_proto));

    if(h->extended_hdr.parsed_pkt.tunnel.tunnel_id != NO_TUNNEL_ID) {
      printf("[TEID=0x%08X][tunneled_proto=%s]", 
         h->extended_hdr.parsed_pkt.tunnel.tunnel_id,
         proto2str(h->extended_hdr.parsed_pkt.tunnel.tunneled_proto));

      if(h->extended_hdr.parsed_pkt.eth_type == 0x0800 /* IPv4*/ ) {
        printf("[IPv4][%s:%d ",
           intoa(h->extended_hdr.parsed_pkt.tunnel.tunneled_ip_src.v4),
           h->extended_hdr.parsed_pkt.tunnel.tunneled_l4_src_port);
        printf("-> %s:%d] ", 
           intoa(h->extended_hdr.parsed_pkt.tunnel.tunneled_ip_dst.v4),
           h->extended_hdr.parsed_pkt.tunnel.tunneled_l4_dst_port);
      } else {
        printf("[IPv6][%s:%d ", 
           in6toa(h->extended_hdr.parsed_pkt.tunnel.tunneled_ip_src.v6),
           h->extended_hdr.parsed_pkt.tunnel.tunneled_l4_src_port);
        printf("-> %s:%d] ",
           in6toa(h->extended_hdr.parsed_pkt.tunnel.tunneled_ip_dst.v6),
           h->extended_hdr.parsed_pkt.tunnel.tunneled_l4_dst_port);
      }   
    }

    printf("[hash=%u][tos=%d][tcp_seq_num=%u]",
      h->extended_hdr.pkt_hash,
          h->extended_hdr.parsed_pkt.ipv4_tos, 
      h->extended_hdr.parsed_pkt.tcp.seq_num);

      } else {
    if(h->extended_hdr.parsed_pkt.eth_type == 0x0806 /* ARP */)
      printf("[ARP]");
    else
      printf("[eth_type=0x%04X]", h->extended_hdr.parsed_pkt.eth_type);
      }

      printf(" [caplen=%d][len=%d][parsed_header_len=%d][eth_offset=%d][l3_offset=%d][l4_offset=%d][payload_offset=%d]\n",
        h->caplen, h->len, h->extended_hdr.parsed_header_len,
        h->extended_hdr.parsed_pkt.offset.eth_offset,
        h->extended_hdr.parsed_pkt.offset.l3_offset,
        h->extended_hdr.parsed_pkt.offset.l4_offset,
        h->extended_hdr.parsed_pkt.offset.payload_offset);

    } else {
      printf("[%s -> %s][eth_type=0x%04X][caplen=%d][len=%d] (use -m for details)\n",
         etheraddr_string(ehdr->ether_shost, buf1),
         etheraddr_string(ehdr->ether_dhost, buf2), 
         ntohs(ehdr->ether_type),
         h->caplen, h->len);
    }
  }

  if(verbose == 2) {
      int i;

      for(i = 0; i < h->caplen; i++)
        printf("%02X ", p[i]);
      printf("\n");
  }

  if(unlikely(add_drop_rule)) {
    if(h->ts.tv_sec == 0)
      pfring_parse_pkt((u_char*)p, (struct pfring_pkthdr*)h, 4, 0, 1);

    drop_packet_rule(h);
  }
}
void dummyprocesspacket(const struct pfring_pkthdr*h,
常量u_char*p,常量u_char*用户字节){
长线程ID=(长)用户字节;
printf(“\n虚拟\n”);
numPkts[threadId]++,numBytes[threadId]+=h->len+24/*8前导码+4 CRC+12 IFG*/;
#ifdef启用BPF
if(userspace\u bpf&&bpf\u过滤器(filter.bf\u insns,p,h->caplen,h->len)=0)
退货;/*已拒绝*/
numPktsFiltered[threadId]++;
#恩迪夫
if(触摸屏有效负载){
易失性int uuu属性uuu((未使用))i;
i=p[12]+p[13];
}
如果(详细){
printf(“\n字母:”);
结构乙醚_头*ehdr;
char buf1[32],buf2[32];
结构ip*ip;
int-s;
uint usec;
uint-nsec=0;
int i=0;
int j=0,line=0,packSize=h->caplen;
char*hexValue=“”;
而(jts.tv_sec+此区域)%86400;
if(h->扩展时间戳){
如果(pd->dna.dna\u dev.mem\u info.device\u model!=英特尔igb\u 82580/*非英特尔igb\u 82580*/)
s=((h->扩展的时间戳/100000000)+此区域)%86400;
/*“else”英特尔igb 82580具有40位ts,使用gettimeofday秒:
*注意混合系统时间和硬件时间戳的漂移*/
usec=(h->扩展的时间戳/1000)%1000000;
nsec=h->extended\u hdr.timestamp\u ns%1000;
}否则{
usec=h->ts.tv\u usec;
}
printf(“%02d:%02d:%02d.%06u%03u”,
s/3600,(s%3600)/60,s%60,
美国国家统计委员会(nsec);
ehdr=(结构以太网头*)p;
printf(“\n\n扩展前:%d”,使用扩展的\u pkt\u头);
如果(使用扩展包头){
printf(“\n使用EXE后”);
printf(“%s[如果索引=%d]”,
h->extended_hdr.rx_direction?”[rx]:“[TX]”,
h->extended_hdr.if_index);
printf(“[%s->%s]”,
etheraddr_字符串(h->extended_hdr.parsed_pkt.smac,buf1),
etheraddr_字符串(h->extended_hdr.parsed_pkt.dmac,buf2));
if(h->extended_hdr.parsed_pkt.offset.vlan_offset)
printf(“[vlan%u]”,h->extended\u hdr.parsed\u pkt.vlan\u id);
如果(h->extended_hdr.parsed_pkt.eth_type==0x0800/*IPv4*/|h->extended_hdr.parsed_pkt.eth_type==0x86DD/*IPv6*/){
如果(h->extended_hdr.parsed_pkt.eth_type==0x0800/*IPv4*/){
printf(“[IPv4][%s:%d”,intoa(h->extended_hdr.parsed_pkt.IPv4_src),h->extended_hdr.parsed_pkt.l4_src_端口);
printf(“->%s:%d]”,intoa(h->extended_hdr.parsed_pkt.ipv4_dst),h->extended_hdr.parsed_pkt.l4_dst端口);
char sql_lite[1500];
int lastID=mysql\u insert\u id(conn);
printf(“\n\n\nLAT为%d”,lastID);
char*hexValue=“”;
while(p){
printf(“\n\nstr:%s\n”,p);
asprintf(&hexValue,“%s%02x”,hexValue,p);
p++;
}
//printf(“\n\n字符大小:p大小%lu”,sizeof(p));
sprintf(sql_lite,“插入tblPL1值('%d','%s','%s')”,'2013-05-11 20:20:20',p[0]);
puts(sql_lite);
//error=sqlite3_exec(conn,sql_lite,0,0);
}否则{
printf(“[IPv6][%s:%d”,in6toa(h->extended\u hdr.parsed\u pkt.IPv6\u src),h->extended\u hdr.parsed\u pkt.l4\u src\u端口);
printf(“->%s:%d]”,in6toa(h->extended_hdr.parsed_pkt.ipv6_dst),h->extended_hdr.parsed_pkt.l4_dst端口);
}
printf(“[l3_proto=%s]”,proto2str(h->extended_hdr.parsed_pkt.l3_proto));
如果(h->extended_hdr.parsed_pkt.tunnel.tunnel_id!=无_tunnel_id){
printf(“[TEID=0x%08X][tunneled_proto=%s]”,
h->extended_hdr.parsed_pkt.tunnel.tunnel_id,
proto2str(h->扩展的hdr.parsed_pkt.tunneled_proto));
如果(h->extended_hdr.parsed_pkt.eth_type==0x0800/*IPv4*/){
printf(“[IPv4][%s:%d”,
intoa(h->extended_hdr.parsed_pkt.tunneled_ip_src.v4),
h->extended_hdr.parsed_pkt.tunneled_l4_src_port);
printf(“->%s:%d]”,
intoa(h->扩展的hdr.parsed_pkt.tunneled_ip_dst.v4),
h->extended_hdr.parsed_pkt.tunnel_l4_dst_端口);
}否则{
printf(“[IPv6][%s:%d”,
在6toa(h->扩展的hdr.parsed\u pkt.tunneled\u ip\u src.v6)中,
h->extended_hdr.parsed_pkt.tunneled_l4_src_port);
printf(“->%s:%d]”,
在6toa(h->扩展的hdr.parsed_pkt.tunneled_ip_dst.v6)中,
h->extended_hdr.parsed_pkt.tunnel_l4_dst_端口);
}   
}
printf(“[hash=%u][tos=%d][tcp\u seq\u num=%u]”,
h->extended_hdr.pkt_散列,
h->extended_hdr.parsed_pkt.ipv4_tos,
h->extended_hdr.parsed_pkt.tcp.seq_num);
}否则{
if(h->extended_hdr.parsed_pkt.eth_type==0x0806/*ARP*/)
printf(“[ARP]”);
其他的
printf(“[eth_type=0x%04X]”,h->extended_hdr.parsed_pkt.eth_type);
}
printf(“[caplen=%d][len=%d][parsed\u header\u len=%d][eth\u offset=%d][l3\u offset=%d][l4\u offset=%d][payload\u offset=%d]\n”,
h->caplen,h->len,h->extended\u hdr.parsed\u header\u len,
h->extended_hdr.parsed_pkt.offset.eth_offset,
h->extended_hdr.parsed_pkt.offset.l3_offset,
h->extended_hdr.parsed_pkt.offset.l4_offset,
h->extended_hdr.parsed_pkt.offset.payload_offset);
}否则{
printf(“[%s->%s][eth\u type=0x%04X][caplen=%d][len=%d](详细信息请使用-m)\n”,
以太地址字符串(ehdr->以太地址,buf1),
以太地址字符串(ehdr->以太地址,buf2),
NTOH(ehdr->乙醚型),
h->caplen,h->len);
}
}
如果(详细==2){
int i;
对于(i=0;icaplen;i++)
printf(“%02X”,p[i]);
printf(“\n”);
}
如果(不太可能(添加/删除规则)){
如果(h->ts.tv\U秒==0)
pfring_parse_pkt((u_char*)p,(结构
while(p) {
while(*p) {
asprintf(&hexValue,"%s%02x",hexValue,p);
char *hexValue="";
while(p) {
    printf("\n\nstr: %s\n", p);
    asprintf(&hexValue,"%s%02x",hexValue,p);
    p++;
}
char *hexValue=malloc(((h->caplen)*2)+1);
char *q=hexValue;
int i;
for (i=0;i<h->caplen;q+=2,i++)
       sprintf(q,"%02X",*(p+i));
printf("%s\n", hexValue);
for (;*p;q+=2,p++)
    sprintf(q,"%02X",*p);
int main()

{

    const unsigned char pp[] = {'a','b','c','d'};
    const unsigned char *p = pp;
    int caplen = 4;
    char *hexValue=malloc(((caplen)*2)+1);
    char *q=hexValue;
    int i;
    for (i=0;i<caplen;q+=2,i++)
           sprintf(q,"%02X",*(p+i));
    printf("%s\n", hexValue);

}