0

Before(using ASCII) i was using std::string as buffer like this:

std::string test = ""; int value = 6; test.append("some string"); test.append((char*)value, 4); test.append("some string"); 

with expected value in test:

"some srtring\x6\x0\x0\x0somestring"

Now i am tring to use Unicode and i wanna keep the same "code" but trubles happens:

std::wstring test = ""; int value = 6; test.append("some string"); test.append((wchar_t*)value, 4); (buffer overflow cause reading 8 bytes) test.append("some string"); 

How can i append bytes like in std::string? Doing:

std::wstring test = ""; int value = 6; test.append("some string"); test.append((wchar_t*)value, 2); test.append("some string"); 

Solve partially the problem cause after i can't append bools.

EDIT: i can even use wstringstream if a binary copy is applied.(normally not)

7
  • 3
    You don't "use Unicode" by just flipping to std::wstring. Commented Mar 31, 2017 at 13:58
  • 1
    Why don't you use std::to_string and std::to_wstring? (and use + for concat) Commented Mar 31, 2017 at 14:00
  • Casts are usually a bad idea. C-style casts are usually an even worse idea. Had you used a C++ cast, you'd find that static_cast<char*>(value) won't compile. Generally, a static_cast is at least safe. By not compiling, at least you might be deterred from trying to cast like that. (char*)value takes the bytes and reinterprets them as characters. Are you sure that's what you really want? Of course not, but the cast doesn't tell you that, like reinterpret_cast does. Commented Mar 31, 2017 at 14:10
  • i need a "binary" dynamic buffer, where i can add the real size of types(bool 1, int 4 etc) Commented Mar 31, 2017 at 14:28
  • @BoundaryImposition: And you don't use ASCII by using std::string, either :) Commented Mar 31, 2017 at 14:30

3 Answers 3

1

You're confusing unicode and character encodings. An std::string can represent unicode code points just fine, using the UTF-8 encoding.

Windows uses the UTF-16LE (or UTF-16 with a BOM, I believe) encoding to represent unicode glyphs. Most others use UTF-8.

An std::string which is encoded in UTF-8 and which uses only ASCII characters can actually be interpreted as an ASCII string. This is the beauty of UTF-8. It's a natural extension.

Anyway,

i need a "binary" dynamic buffer, where i can add the real size of types(bool 1, int 4 etc)

An std::vector<uint8_t> is probably more suitable for this task. It communicates that it is not something human-readable, per se. If you need to embed strings into this buffer, make sure that sizeof(char) == sizeof(uint8_t) on the platform, and then just write the data as-is to this buffer.

If you're saving this buffer on one machine and try to read it on another machine, you have to take care of endianness too.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your clarification. I'll search more about that.
0

You make a function that reads the stuff you want to put:

void putBytes(std::wstring& s, char* c, int numBytes) { while (numBytes-- > 0) s += (wchar_t)*c++; } 

Then you can call it:

int value = 65; putBytes(s, reinterpret_cast<char*>(&value), sizeof(value)); 

Comments

0

I think a IStream is a proper way to do this...i'll make an interface to handle different types. I was abusing std::string for an easy "dynamic binary array", with std::wstring this is not possible,for many reasons but most silly one is that require at least 2 bytes, so no room for a bool

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.