我在 Tizen 中开发了一个用于 Http 请求和响应的应用程序。我能够成功发布并获得回复。(已检查正文长度)。 我得到的响应在 ByteBuffer 中。
ByteBuffer* pBuffer = pHttpResponse->ReadBodyN();
我的类型转换有点差。我想把这个 ByteBuffer 转换成一个字符串,这样我就可以在 Label 中设置。
最佳答案
ByteBuffer
中的数据是否以零结尾的 ASCII?在这种情况下,您可以像这样创建字符串:
String str((const char*)(byteBuf.GetPointer()));
否则,您可以使用 Tizen::Text::Encoding 解码 ByteBuffer
,只要你知道编码是什么。例如:
// Construct some test data. In your case the buffer would come
// as a HTTP response.
char chars[] = "\xE5\xE6\xF6"; // æåø in ISO-8859-1
ByteBuffer byteBuf;
byteBuf.Construct((byte*)chars, 0, 3, 3);
Encoding* pEnc = Encoding::GetEncodingN(L"ISO-8859-1");
String str;
pEnc->GetString(byteBuf, 0, byteBuf.GetRemaining(), str);
Label *pLabel = static_cast<Label*>(GetControl(IDC_LABEL1));
pLabel->SetText(str);
关于c++ - 字节缓冲区到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17161146/