Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing istringstream to process a memory block of variable length
    primarykey
    data
    text
    <p>I'm trying to use <code>istringstream</code> to recreate an encoded <code>wstring</code> from some memory. The memory is laid out as follows:</p> <ol> <li>1 byte to indicate the start of the wstring encoding. Arbitrarily this is '!'.</li> <li>n bytes to store the character length of the string in text format, e.g. 0x31, 0x32, 0x33 would be "123", i.e. a 123-character string</li> <li>1 byte separator (the space character)</li> <li>n bytes which are the wchars which make up the string, where wchar_t's are 2-bytes each.</li> </ol> <p>For example, the byte sequence:</p> <p><strong>21 36 20 66 00 6f 00 6f 00</strong></p> <p>is "!6 f.o.o." (using dots to represent char 0)</p> <p>All I've got is a char* pointer (let's call it <code>pData</code>) to the start of the memory block with this encoded data in it. What's the 'best' way to consume the data to reconstruct the wstring ("foo"), and also move the pointer to the next byte past the end of the encoded data?</p> <p>I was toying with using an istringstream to allow me to consume the prefix byte, the length of the string, and the separator. After that I can calculate how many bytes to read and use the stream's <code>read()</code> function to insert into a suitably-resized wstring. <strong>The problem is, how do I get this memory into the istringstream in the first place?</strong> I <em>could</em> try constructing a string first and then pass that into the istringstream, e.g.</p> <pre><code>std::string s((const char*)pData); </code></pre> <p>but that doesn't work because the string is truncated at the first null byte. Or, I could use the string's other constructor to explicitly state how many bytes to use:</p> <pre><code>std::string s((const char*)pData, len); </code></pre> <p>which works, but only if I know what <code>len</code> is beforehand. That's tricky given that the data is variable length.</p> <p>This seems like a really solvable problem. Does my rookie status with strings and streams mean I'm overlooking an easy solution? Or am I barking up the wrong tree with the whole string approach?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload