2016-09-05 2 views
-1

다음 함수는 문자 * 반환어떻게 수 std의 char *를 변환 : : 문자열

// Returns the pointer to the actual packet data. 
// @param pkt_handle The buffer handle. 
// @param queue  The queue from which the packet is arrived or destined. 
// @return   The pointer on success, NULL otherwise. 
u_char * pfring_zc_pkt_buff_data(
    pfring_zc_pkt_buff *pkt_handle, 
    pfring_zc_queue *queue 
); 

을 다음과 같은 기능이 입력으로 고통을합니다.

template<class ...Args> 
    bool write(Args&&... recordArgs) { 
    auto const currentWrite = writeIndex_.load(std::memory_order_relaxed); 
    auto nextRecord = currentWrite + 1; 
    if (nextRecord == size_) { 
     nextRecord = 0; 
    } 
    if (nextRecord != readIndex_.load(std::memory_order_acquire)) { 
     new (&records_[currentWrite]) T(std::forward<Args>(recordArgs)...); 
     writeIndex_.store(nextRecord, std::memory_order_release); 
     return true; 
    } 

    // queue is full 
    return false; 
    } 

기능을 변경할 수 없습니다. u_char * pfring_zc_pkt_buff_datapfring_zc_recv_pkt(zq, &buffers[lru], 1)에 의해 캡처 된 실제 패킷에 대한 포인터를 반환합니다.

패킷의 일부를 버퍼에 저장해야합니다. 내가 문자열의 char *로 변환해야 * 같은 많은 유사한 질문 .there 있습니다 :

How to Convert unsigned char* to std::string in C++?

How to convert a const char * to std::string

How to convert char to string?

내 질문에 대한 적절한 답을 찾을 수 없습니다.

여기 내 코드 중 일부입니다.

void run() { 
    int i=0,n; 
    char buf[_snapLengthConnection + _ConnectionHeaderSize];  
    for(;;){ 
     if(likely(pfring_zc_recv_pkt(zq, &buffers[lru], wait_for_packet)>= 0)) {    
      u_char *pkt_data = pfring_zc_pkt_buff_data(buffers[lru], zq);   
      pfring_print_pkt(buf, sizeof(buf), pkt_data, buffers[lru]->len,sizeof(buf));  

      std::string *payload; 

      //need to save buf to payload 
      // how do i copy buf to payload?    
      std::cout<< payload<<"=="<<std::endl;    

      _activeBuffer->queue->write(payload);  
      lru++; lru &= NBUFFMASK;    
     } 
    } 
    LOG_INFO("capture_loop for connections: Exiting");   

} 

어떻게 char *을 문자열로 변환 할 수 있습니까?

OS : 우분투 GCC 버전 : 그렇지 않으면 정의되지 않은 동작으로 이어질 것입니다,

const char *str = "Shravan Kumar"; 
std::string str(str); 

은 그냥 char *하지 NULL 있는지 확인 : 4.8.2

+1

'std :: string *'이 무슨 뜻입니까? – Shravan40

+0

@ Shravan40. 질문이 편집되었습니다 –

+0

@ m.r226 데이터의 길이를 알고 계십니까? –

답변

1

std::string이의 생성자가 .

+0

char *가 NULL이 아니며 내용을 인쇄 할 수 있습니다. 전에 이것을 테스트했습니다. –

+0

이것은 패킷 데이터가 아닌 C 스타일 문자열에서만 작동합니다. 'str'의 생성자는 패킷 데이터의 길이를 어떻게 알 수 있습니까? –

관련 문제