2009-05-22 81 views
5

我對C++比較陌生。最近的分配要求我將大量字符緩衝區(從結構/套接字等)轉換爲字符串。我一直在使用以下變體,但看起來很尷尬。有沒有更好的方式來做這種事情?將char數組緩衝區轉換爲字符串的好方法?

#include <iostream> 
#include <string> 

using std::string; 
using std::cout; 
using std::endl; 


char* bufferToCString(char *buff, int buffSize, char *str) 
{ 
    memset(str, '\0', buffSize + 1); 
    return(strncpy(str, buff, buffSize)); 
} 


string& bufferToString(char* buffer, int bufflen, string& str) 
{ 
    char temp[bufflen]; 

    memset(temp, '\0', bufflen + 1); 
    strncpy(temp, buffer, bufflen); 

    return(str.assign(temp)); 
} 



int main(int argc, char *argv[]) 
{ 
    char buff[4] = {'a', 'b', 'c', 'd'}; 

    char str[5]; 
    string str2; 

    cout << bufferToCString(buff, sizeof(buff), str) << endl; 

    cout << bufferToString(buff, sizeof(buff), str2) << endl; 

} 

回答

15

鑑於你的輸入字符串不是空終止的,你不應該使用str ...函數。你也不能使用普遍使用的std::string constructors。但是,您可以使用此構造函數:

std::string str(buffer, buflen):需要char*和長度。 (實際上const char*和長度)

我會避免C字符串版本。這將使:

std::string bufferToString(char* buffer, int bufflen) 
{ 
    std::string ret(buffer, bufflen); 

    return ret; 
} 

如果你真的必須使用C-string版本,無論是在bufflen位置掉落0(如果可以的話),或創建一個緩衝的bufflen+1,然後memcpy緩衝成這樣了,落個0結尾(bufflen位置)。

1

的std :: string爲const char *:

my_str.c_str(); 

的char *到的std :: string:

string my_str1 ("test"); 
    char test[] = "test"; 
    string my_str2 (test); 

甚至

string my_str3 = "test"; 
+0

問題是緩衝區沒有終止空值。 – 2009-05-22 02:20:22

+0

使用常量字符串初始化char數組時,它會得到一個終止的空值。當你使用string :: c_str()時,你也會得到一個終止的null。我不明白你的投訴是什麼。 – 2009-05-22 02:27:56

+0

答案中的代碼是正確的,但與問題不同。 「char buff [4] = {'a','b','c','d'};」不會給你一個以null結尾的字符串。 – markh44 2009-05-22 08:02:32

0
std::string buf2str(const char* buffer) 
{ 
    return std::string(buffer); 
} 

Ø r只是

std::string mystring(buffer); 
+0

我一直在尋找一種更簡潔的方式來添加一個終止的null,以便像這樣的東西能正常工作。 – 2009-05-22 02:21:40

1

如果數據緩衝區中可能有空('\ 0')字符,則不需要使用以空值終止的操作。

您可以使用帶char *,length的構造函數。

char buff[4] = {'a', 'b', 'c', 'd'}; 
cout << std::string(&buff[0], 4); 

或者你可以使用任何將範圍的構造:

cout << std::string(&buff[0], &buff[4]); // end is last plus one 

請勿使用的buff []數組以上的的std :: string(BUFF)構造函數,因爲它不是空封端的。

0

使用字符串構造函數的大小:

string (const char * s, size_t n); 

的內容被初始化爲第一n由s指出字符 陣列 字符在所形成的字符串的副本。

cout << std::string(buff, sizeof(buff)) << endl; 

http://www.cplusplus.com/reference/string/string/string/

非空值終止緩衝至C字符串:

memcpy(str, buff, buffSize); 
str[bufSize] = 0; // not buffSize+1, because C indexes are 0-based. 
0

的方法需要知道字符串的大小。你必須要麼:

  1. 在字符的情況下,*傳遞長度 方法
  2. 在字符的情況下,*指向空 終止字符數組,你可以 使用了一切爲null 字符
  3. 對於char []您可以使用模板 弄不清的char []

1)例如大小 - 對於那些你傳遞的bufflen情況:

std::string bufferToString(char* buffer, int bufflen) 
{ 
    return std::string(buffer, bufflen); 
} 

2)例如 - 對於其中緩衝液是指向空字符的終止陣列情況:

std::string bufferToString(char* buffer) 
{ 
    return std::string(buffer); 
} 

3)例如 - 對於其中傳遞的char []例:

template <typename T, size_t N> 
std::string tostr(T (&array)[N]) 
{ 
    return std::string(array, N); 
} 


Usage: 
char tstr[] = "Test String"; 
std::string res = tostr(tstr); 
std::cout << res << std::endl; 

對於前兩種情況,實際上並不需要創建新方法:

std::string(buffer, bufflen); 
std::string(buffer); 
0

字符串值(reinterpret_cast(buffer),length);

相關問題