views:

60

answers:

2

I need to create a wstring with the chars that have the following ascii values: 30, 29, 28, 27, 26, 25.

In VB6, I would do asc(30) + asc(29)+ etc...

What's the C++ equivalent?

Thanks!

+1  A: 

An std::wstring is nothing more than an std::vector disguised as a string.

Therefore you should be able to use the push_back method, like this:

std::wstring s;

s.push_back(65);
s.push_back(0);

std::wcout << s << std::endl;

Don't forget the 0-terminator !

Patrick
I was about to forget it, thanks. O:-)
Fernando
Um... A string is a bit different than a vector, but they both follow Standard Collection interface guidelines.
James Curran
You do *not* have to add a zero-terminator. Put in the contents of the string. When/if you want a zero-terminated C-style string, its `c_str()` member will give you that.
Jerry Coffin
Actually, according to the current standard, `basic_string<char_t>` is NOT required to have contiguous storage -- it could be implemented more like a rope instead of a vector and still be legal.
Billy ONeal
+2  A: 

Is this a trick question about character set conversion? :) Because the standard does not guarantee that an ASCII character is represented by its ASCII integer value in a wchar_t (even though for most compilers/systems, this will be true). If it matters, explicitly widen your char using an appropriate locale:

std::wstring s;
std::locale loc("C"); // pick a locale with ASCII encoding

s.push_back(std::use_facet<std::ctype<wchar_t> >(loc).widen(30));
s.push_back(std::use_facet<std::ctype<wchar_t> >(loc).widen(29));
s.push_back(std::use_facet<std::ctype<wchar_t> >(loc).widen(28));

Don't terminate with a trailing 0, it is added when you convert the wstring to a wchar_t * by invoking .c_str()