Rectangle 27 0

std::wstring StringToWString(const std::string& s)
 {
 std::wstring temp(s.length(),L' ');
 std::copy(s.begin(), s.end(), temp.begin());
 return temp; 
 }

And here's the original forum post with a possible second solution using the windows API function MultiByteToWideChar:

Why not ' '? it will be erased anyway by the copy function. It's just an arbitrary char to make room for the string.

Then why would you initialize these characters to ' ' when you know that they are going to be rewritten?

because the copy function does not create a buffer. The buffer needs to be created first, which is done by the constructor.

Ah, sorry I see now. I thought that basic_string has also constructor that takes only size_type count, just like std::vector has.

c++ - Convert const char* to wstring - Stack Overflow

c++
Rectangle 27 0

On OS X wstring uses UTF-32 rather than UTF-16. You can do the conversion like this:

#include <codecvt>
#include <string>

// make facets usable by giving them a public destructor
template <class Facet>
class usable_facet
    : public Facet
{
public:
    template <class ...Args>
        usable_facet(Args&& ...args)
            : Facet(std::forward<Args>(args)...) {}
    ~usable_facet() {}
};

std::wstring s2ws(std::string const &s) {
    std::wstring_convert<
        usable_facet<std::codecvt<char32_t,char,std::mbstate_t>>
        ,char32_t> convert;
    std::u32string utf32 = convert.from_bytes(s);
    static_assert(sizeof(wchar_t)==sizeof(char32_t),"char32_t and wchar_t must have same size");
    return {begin(utf32),end(utf32)};
}

c++ - Convert const char* to wstring - Stack Overflow

c++
Rectangle 27 0

In addition to the other answers, you could use a trick from Microsoft's book (specifically, tchar.h), and write something like this:

# ifdef APP_USE_UNICODE
    typedef std::wstring AppStringType;
    #define _T(s) (L##s)
# else
    typedef std::string  AppStringType;
    #define _T(s) (s)
# endif

AppStringType foo = _T("hello world!");

(Note: my macro-fu is weak, and this is untested, but you get the idea.)

c++ - Assigning a "const char*" to std::string is allowed, but assigni...

c++ gcc stl g++
Rectangle 27 0

Both works[5] and doesntWork[5] cause undefined behavior. The buffers are only valid up to [4], and [5] is outside that.

const wchar_t *works = L"Test";

works[0] -> L'T'
works[1] -> L'e'
works[2] -> L's'
works[3] -> L't'
works[4] -> L'\0'
works[5] -> undefined behavior, the program is broken if it ever tries to access this

c++ - wstring to const wchar* using c_str() - Stack Overflow

c++ const wstring wchar
Rectangle 27 0

AFAIK this works only from C++11 and above:

std::wstring stringToWstring(const std::string& t_str)
{
    //setup converter
    typedef std::codecvt_utf8<wchar_t> convert_type;
    std::wstring_convert<convert_type, wchar_t> converter;

    //use converter (.to_bytes: wstr->str, .from_bytes: str->wstr)
    return converter.from_bytes(t_str);
}

c++ - Convert const char* to wstring - Stack Overflow

c++
Rectangle 27 0

You can convert a std::wstring to a const wchar_t * using the c_str member function :

std::wstring s;
const wchar_t *str = s.c_str();

However, a conversion to a const char * isn't natural : it requires an additional call to std::wcstombs.

can you give a full example including wcstombs? I'm trying to solve this the way you suggest.

convert std::wstring to const *char in c++ - Stack Overflow

c++
Rectangle 27 0

You cannot do this just like that. std::wstring represents a string of wide (Unicode) characters, while char* in this case is a string of ASCII characters. There has to be a code page conversion from Unicode to ASCII.

To make the conversion you can use standard library functions such as wcstombs, or Windows' WideCharToMultiByte function.

wcrtomb
wcsrtombs

@Yossarian: The problem with the standard library's functions is that the standard doesn't define which wide character set, nor does it define which multi byte character set, are used in wcstombs. If you need to get the string in a specific encoding (such as UTF-8), then you have to write the conversion yourself, or use a platform specific extension (such as WideCharToMultiByte)

std::string can contain unicode via UTF-8 and std::wstring is not married to UTF-16/UTF-32. So this this is a very misleading answer.

convert std::wstring to const *char in c++ - Stack Overflow

c++
Rectangle 27 0

I recommend you using std::string instead of C-style strings (char*) wherever possible. You can create std::string object from const char* by simple passing it to its constructor.

Once you have std::string, you can create simple function that will convert std::string containing multi-byte UTF-8 characters to std::wstring containing UTF-16 encoded points (16bit representation of special characters from std::string).

There are more ways how to do that, here's the way by using MultiByteToWideChar function:

std::wstring s2ws(const std::string& str)
{
    int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
    std::wstring wstrTo( size_needed, 0 );
    MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
    return wstrTo;
}

Disclaimer: MultiByteToWideChar is a Windows-only function. (OP is using Windows but question is tagged just c++)

c++ - Convert const char* to wstring - Stack Overflow

c++
Rectangle 27 0

As twalberg points out, the most important bit of the compiler error message is the "loses qualifiers" bit. It also tells you that it can't convert from const std::wstring to std::wstring&, except that it expanded the first std::wstring into its full template instantiation form.

The issue is that your DeleteCtl takes the argument by non-const reference, as if it wants to modify the string there (bad idea), but it can't do that, because you're iterating over a set, and you cannot change the members of a set once they're in there (std::set doesn't make a difference between const_iterator and iterator, essentially). The reason is that std::set bases its internal structure on the values of its elements, and if you change those values, the internal structure becomes invalid, and horrible things happen.

As you said I change the prototype of DeleteCtl to const reference and build successfully.Thanx for that.But now There is a Intellisense error is displaying what does it mean IntelliSense: qualifiers dropped in binding reference of type "std::wstring &" to initializer of type "const std::wstring

Kindly provide the solution @Sebastian Redl

c++ - error C2664: cannot convert parameter 1 from 'const std::basic_s...

c++
Rectangle 27 0

I recommend you using std::string instead of C-style strings (char*) wherever possible. You can create std::string object from const char* by simple passing it to its constructor.

Once you have std::string, you can create simple function that will convert std::string containing multi-byte UTF-8 characters to std::wstring containing UTF-16 encoded points (16bit representation of special characters from std::string).

There are more ways how to do that, here's the way by using MultiByteToWideChar function:

std::wstring s2ws(const std::string& str)
{
    int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
    std::wstring wstrTo( size_needed, 0 );
    MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
    return wstrTo;
}

Disclaimer: MultiByteToWideChar is a Windows-only function. (OP is using Windows but question is tagged just c++)

It would be best if your solution is cross platform.

c++ - Convert const char* to wstring - Stack Overflow

c++
Rectangle 27 0

On OS X wstring uses UTF-32 rather than UTF-16. You can do the conversion like this:

#include <codecvt>
#include <string>

// make facets usable by giving them a public destructor
template <class Facet>
class usable_facet
    : public Facet
{
public:
    template <class ...Args>
        usable_facet(Args&& ...args)
            : Facet(std::forward<Args>(args)...) {}
    ~usable_facet() {}
};

std::wstring s2ws(std::string const &s) {
    std::wstring_convert<
        usable_facet<std::codecvt<char32_t,char,std::mbstate_t>>
        ,char32_t> convert;
    std::u32string utf32 = convert.from_bytes(s);
    static_assert(sizeof(wchar_t)==sizeof(char32_t),"char32_t and wchar_t must have same size");
    return {begin(utf32),end(utf32)};
}

c++ - Convert const char* to wstring - Stack Overflow

c++
Rectangle 27 0

Looks like you can do something like this:

#include <sstream>
    // ...
    std::wstringstream tmp;
    tmp << "hello world";
    std::wstring our_string =

Although for a more complex situation, you may want to break down and use mbstowcs

c++ - Assigning a "const char*" to std::string is allowed, but assigni...

c++ gcc stl g++
Rectangle 27 0

wchar_t is 32bit on OSX, thus std::wstring can hold a UTF-32 encoded string:

std::wstring str = ...;
CFStringRef encoded = CFStringCreateWithBytes(
                                     kCFAllocatorDefault,
                                     reinterpret_cast<uint8*>(str.c_str()),
                                     str.size() * sizeof(wchar_t),
                                     kCFStringEncodingUTF32LE,
                                     false);

c++ - Convert "wstring" to "const UInt8 *" - Stack Overflow

c++ xcode osx unicode wstring
Rectangle 27 0

You should use the *16 sqlite functions (e.g. sqlite3_prepare16) where you4 can give UTF-16 (i.e. wstring) as parameters. Don_t forget to use 2*wcslen as length of the string. If you insist on the const char* functions, you have to convert to UTF-8 first.

If you're writing a cross-platform application, be aware that wchar_t is not necessarily a UTF-16 code unit. Of course, it is on Windows.

Why do I need to multiply the length of the string by two?

Because wcslen gives the length in number of wchar_t characters, which are 2 bytes, and sqlite wants the length in bytes.

c++ - function to convert wstring to const char * for SQlite - Stack O...

c++ visual-studio-2010 sqlite wstring
Rectangle 27 0

You need a library that can encode/decode UTF8. Unfortunately, this functionality isn't included with the std c++ library. Here's one library you might use: http://utfcpp.sourceforge.net/

Here's an example use of it:

utf8::utf8to32(bytes.begin(), bytes.end(), std::back_inserter(wstr));

c++ - Convert const char* to wstring - Stack Overflow

c++
Rectangle 27 0

You can convert char string to wstring directly as following code:

char buf1[] = "12345678901234567890";
wstring ws(&buf1[0], &buf1[20]);

c++ - Convert const char* to wstring - Stack Overflow

c++
Rectangle 27 0

To convert from a multibyte encoding to a wide character encoding, take a look at the header <locale> and the type std::codecvt. The Dinkumware library has a class Dinkum::wstring_convert that makes performing such multibyte-to-wide conversions easier.

The function std::codecvt_byname allows one to find a codecvt instance for a particular named encoding. Unfortunately, discovering the names of the encodings (or locales) on your system is implementation-specific.

c++ - Assigning a "const char*" to std::string is allowed, but assigni...

c++ gcc stl g++
Rectangle 27 0

#include <tchar.h>

tstring instead of wstring/string TCHAR* instead of char* and _T("hello") instead of "hello" or L"hello"

this will use the appropriate form of string+char, when _UNICODE is defined.

"(environment: gcc-4.4.1 on Ubuntu Karmic 32bit)" There is no tchar.h on my Karmic system. I'm pretty sure it's Windows-specific...

The problem is sizeof(Windows::wchar_t)=2, sizeof(AllOtherNonWindowsWorld::wchar_t)=4... Also, UTF-8 is generally much more preferred and less error prone.

@Artyom: yes, especially because ASCII is a strict subset of UTF-8. It makes the transition quite a bit simple.

c++ - Assigning a "const char*" to std::string is allowed, but assigni...

c++ gcc stl g++
Rectangle 27 0

IMHO you should use a portable character codec library. Here's an example of minimal portable code using iconv, which should be more than enough. It's supposed to work on Windows (if it does, you can get rid of your windows-specific code altogether). I follow the GNU guidelines not to use the wcstombs & co functions ( https://www.gnu.org/s/hello/manual/libc/iconv-Examples.html ) Depending on the use case, handle errors appropriately... and to enhance performance, you can create a class out of it.

#include <iostream>

#include <iconv.h>
#include <cerrno>
#include <cstring>
#include <stdexcept>

std::string wstring_to_utf8_string(const std::wstring &input)
{
    size_t in_size = input.length() * sizeof(wchar_t);
    char * in_buf = (char*)input.data();
    size_t buf_size = input.length() * 6; // pessimistic: max UTF-8 char size
    char * buf = new char[buf_size];
    memset(buf, 0, buf_size);
    char * out_buf(buf);
    size_t out_size(buf_size);
    iconv_t conv_desc = iconv_open("UTF-8", "wchar_t");
    if (conv_desc == iconv_t(-1))
        throw std::runtime_error(std::string("Could not open iconv: ") + strerror(errno));
    size_t iconv_value = iconv(conv_desc, &in_buf, &in_size, &out_buf, &out_size);
    if (iconv_value == -1)
        throw std::runtime_error(std::string("When converting: ") + strerror(errno));
    int ret = iconv_close(conv_desc);
    if (ret != 0)
        throw std::runtime_error(std::string("Could not close iconv: ") + strerror(errno));
    std::string s(buf);
    delete [] buf;
    return s;
 }


int main() {
    std::wstring in(L"hello world");
    std::wcout << L"input: [" << in << L"]" << std::endl;
    std::string out(wstring_to_utf8_string(in));
    std::cerr << "output: [" << out << "]" << std::endl;
    return 0;
}

IMHO many of the objections against wctombs don't apply to std::locale and co. Using iconv is good advice, though.

c++ - How to make my `std::string url_encode_wstring(const std::wstrin...

c++ linux string urlencode wstring
Rectangle 27 0

1) from_bytes() overload that takes the single const char* expects a null-terminated byte string, but your very second byte is '\0'.

#include <iostream>
#include <locale>
#include <memory>
#include <codecvt>
#include <string>

using namespace std;

int main()
{
    u16string s;

    s.push_back('h');
    s.push_back('e');
    s.push_back('l');
    s.push_back('l');
    s.push_back('o');

    wstring_convert<codecvt_utf16<wchar_t, 0x10ffff, little_endian>,
                     wchar_t> conv;
    wstring ws = conv.from_bytes(
                     reinterpret_cast<const char*> (&s[0]),
                     reinterpret_cast<const char*> (&s[0] + s.size()));

    wcout << ws << endl;

    return 0;
}

Tested with Visual Studio 2010 SP1 on Windows and CLang++/libc++-svn on Linux.

PS, this should be using char32_t to guarantee UCS4, of course. The wchar_t version produces UTF-16 where wchar_t is 16 bit.

This is a very awesome answer, and I salute you for knowing all this really! I'd upvote the answer 3 more times if I could. May I also ask more questions if allowed: 1. Can you tell me the concept of MaxCode which you set to 0x10ffff? Cause I notice that it's actually needed.. 2. Good point about '\0' being the terminator of const char*. Makes me quickly wonder, what would be the corresponding terminator for char16_t* ? Thanks again.

@ryaner Maxcode is just the limit on the acceptable character values, it's only needed here because endianness/BOM handling indicator happens to be the third template parameter, which I think is a small design flaw. The terminating character for a null-terminated array of chat16_t is char16_t() aka u'\0'

c++ - clang: converting const char16_t* (UTF-16) to wstring (UCS-4) - ...

c++ osx unicode c++11 clang