読者です 読者をやめる 読者になる 読者になる

Haruyuki Mohri

毛利春幸のブログです。

10 Seattle UnicodeString To std::wstring in Android/iOS

10Seattle Android/iOS String.w_str() is WideChar*

typedef UnicodeString String;

WideChar is 2Byte.

In the case of iOS/Android std::wstring.c_str() is 4byte wchar_t*

directly copy not possible to UnicodeString from std::wstring.

basic_string.h(565): 候補関数 を呼び出せません。1st 引数を 'WideChar ' (aka 'char16_t ') から 'wchar_t' へ変換する既知の方法がありません。* を使って引数を逆参照してください

So, my idea

std::wstring StrTowstr(String& input)
{
    std::wstring out;
    for (int i=0; i < input.Length(); i++) {
       #ifndef _WIN64
        out +=input[i];
       #else /* _WIN64 */
        out +=input[i+1];
       #endif
    }
    return out;
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
    std::wstring ws;
    String org = Format(L"안녕하세요你好,%f,%f", ARRAYOFCONST((0.5,0.6)) );
    ws = StrTowstr(org);

    Memo1->Lines->Append(ws.c_str());

}

f:id:mojeld:20150918175517j:plain

I was taught from Tomohiro Takahashi san.

How that uses a UCS4String

std::wstring StrTowstr(const UnicodeString& input)
{
    UCS4String ucs4 = UnicodeStringToUCS4String(input);
    std::wstring out((wchar_t*)&ucs4[0], ucs4.get_length());
    return out;
}

It is very smart.