0

I have developed an application for Http Request and Response in Tizen. I am successfully able to post and get the response.(Checked Body length). The response which I got is in ByteBuffer.

ByteBuffer* pBuffer = pHttpResponse->ReadBodyN();

I am little poor in type casting. I want this ByteBuffer converted into a string so that I can set in the Label.

Cyril
  • 1,216
  • 3
  • 19
  • 40

1 Answers1

1

Is the data in the ByteBuffer zero-terminated ASCII? In that case you can create the string like this:

String str((const char*)(byteBuf.GetPointer()));

Otherwise you can decode the ByteBuffer using Tizen::Text::Encoding, as long as you know what the encoding is. For example:

// 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);
Michael
  • 57,169
  • 9
  • 80
  • 125
  • It works if the data is zero-terminated ASCII, so the response you get apparently doesn't meet that criteria. Do you have any idea what encoding it's in? – Michael Jun 18 '13 at 07:44
  • Looks like it's using an 8-bit encoding. There might not be a zero-terminator in the `ByteBuffer` though. You could copy the raw array to another (longer) array and append a zero-terminator. Or you could call `GetByte` in a loop and append each byte to your `String`. – Michael Jun 18 '13 at 09:17
  • Hi. You are right. I am using another link which has no encoding http://www.w3schools.com/xml/singlebyte1.xml. Now I am able to the get the items into a byte array using a loop – Cyril Jun 18 '13 at 09:58
  • But, when I set the bytes to the label _responseLabel.SetText(byteStorage); it is printing in Square square boxes. I don't know why it is printing like that eventhough the xml url has no Encoding. Can you help me please ? – Cyril Jun 18 '13 at 10:00
  • I don't know how you'd solve it if the encoding is unknown. But as long as you know the encoding you can decode the data into a `String` using `Tizen::Text::Encoding`. See my updated answer. – Michael Jun 18 '13 at 10:54