2012-12-14 29 views

回答

3

使用std::stringstream::write(s, length);

注意如果char*是空值終止operator<<()可以用來代替和length並不需要提供:

std::stringstream s; 
const char* buf = "hello"; 
s << buf; 
+0

以及如果我得到void *和它的長度?並希望得到它作爲char *'? – myWallJSON

+0

和char *不是C字符串,而是數組 – myWallJSON

+0

@myWallJSON:如果您有'void * p',那麼在使用'write'之前,您需要將其轉換('static_cast (p)') 。 –