[英]C++: Conversion const uint8_t* to std::stringstream
如何將const uint8_t*
轉換回stringstream
? 謝謝
std::stringstream s1;
s1 << 4 << ' ' << 2 <<;
const uint8_t* sbuffer = (uint8_t*) s1.str().c_str();
int sbufferlength = s1.str().length();
std::stringstream s2 = //get stringstream from sbuffer
int x, y;
s2 >> x >> y;
請注意:您沒有捕獲字符串流的地址,而是接收到指向其內部一部分的指針,該字符串所管理的c字符串,並將其轉換為unit8_t指針。
因此,您不能只是將其回退,而是需要創建第二個字符串流並從c字符串數組構造它。
s2 << sbuffer;
如果你想填充stringstream
與char table
,您可以使用str(const string& s)
這樣做。
在您的情況下:
std::stringstream s2;
s2.str(string((char*)sbuffer,sbufferlength));
或者直接使用構造函數:
std::stringstream s2(string((char*)sbuffer,sbufferlength));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.