Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert const char* to wstring

Tags:

c++

I'm working on a native extension for a zinc based flash application and I need to convert a const char* to a wstring.

This is my code:

mdmVariant_t* appendHexDataToFile(const zinc4CallInfo_t *pCallInfo, int paramCount, mdmVariant_t **params) {

    if(paramCount >= 2) {
        const char *file    = mdmVariantGetString(params[0]);
        const char *data    = mdmVariantGetString(params[1]);

        return mdmVariantNewInt(native.AppendHexDataToFile(file, data));
    }
    else {
        return mdmVariantNewBoolean(FALSE);
    }
}

But native.AppendHexDataToFile() needs two wstring. I'm not very good with C++ and I think all those different string types are totally confusing and I didn't find something useful in the net. So I'm asking you guys how to do it.

Edit: The Strings are UTF-8 and I'm using OSX and Windows XP/Vista/7

like image 775
Johannes Klauß Avatar asked May 24 '12 12:05

Johannes Klauß


4 Answers

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;
}

Check these questions too:
Mapping multibyte characters to their unicode point representation
Why use MultiByteToWideCharArray to convert std::string to std::wstring?

like image 99
LihO Avatar answered Oct 16 '22 15:10

LihO


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

char buf1[] = "12345678901234567890";
wstring ws(&buf1[0], &buf1[20]);
like image 37
SaeidMo7 Avatar answered Oct 16 '22 16:10

SaeidMo7


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

#include <codecvt>

// ...

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);
}

Reference answer

Update

As indicated in the comments, <codecvt> seems to be deprecated in C++17. See here: Deprecated header <codecvt> replacement

like image 16
anhoppe Avatar answered Oct 16 '22 16:10

anhoppe


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));
like image 2
Edward Loper Avatar answered Oct 16 '22 15:10

Edward Loper