Click here to Skip to main content
14,934,114 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
input some Japanese letters in my QR code. in my final result i cant get the whole what i given "12345、エンドコード、6789", but partial output(12345、) will have get.remain part will not me.can anyone help me to get the full input in my final result.

What I have tried:

using the UTF-8 multibytetowidechar conversion method.

std::string &output;
std::string stval((LPCSTR)Out);
std::wstring convertedstring;
int Reqsize = MultiByteToWideChar(CP_UTF8, 0, stval.c_str(), -1, 0, 0);
if (Reqsize > 0)
MultiByteToWideChar(CP_UTF8, 0, stval.c_str(), -1, &bufferString[0], Reqsize);
convertedstring.assign(bufferString.begin(), bufferString.end() - 1);

const wchar_t* szText= convertedstring.c_str();
char *str = new char[1024];
sprintf(str,"%ls", szText);
size_t srtzsie = convertedstring.size();

here str will get 12345、only, finaly i use output.append(str,srtzsie); ,to show the output. i want show full input in my output part.

thanks in advance.
Updated 7-Aug-18 0:30am
Richard Deeming 7-Aug-18 13:44pm
You have already posted this:[^]

If you want to update your question, click the green "Improve question" link and edit your question. DO NOT post the update as a new question.

1 solution

The problem is located in these lines:
const wchar_t* szText= convertedstring.c_str();
char *str = new char[1024];
sprintf(str,"%ls", szText);
Here you are converting the wide char string to multi byte using the current code page by using sprintf with the format %ls. All characters not supported by the current code page will not be shown later when printing the string.

Forget about using multi byte strings for user interaction nowadays. They are even converted back to wide strings when calling Windows API functions because Windows is using Unicode internally since nearly twenty years.

Your code is also much too complicated. If Out is a UTF-8 encoded LPCSTR or LPSTR:
//std::wstring convertedstring;
int reqsize = ::MultiByteToWideChar(CP_UTF8, 0, Out, -1, NULL, 0);
if (reqsize > 0)
    wchar_t *wide = new wchar_t[reqsize];
    ::MultiByteToWideChar(CP_UTF8, 0, Out, -1, wide, reqsize);
    // Use wide here for output

    // Optionally assign it to convertedstring
    //convertedstring = wide;
    delete[] wide;
Member 13323088 9-Aug-18 0:47am
in my result part i get "0" only,not on above mention values.
in "result.append((const char*)convertedstring.c_str());"result values fetching part and this place always get the "0". how can i get output to this part.
Jochen Arndt 9-Aug-18 4:42am
result.append((const char*)convertedstring.c_str()); is not part of my solution.

Do you know what that line does?
Obviously not. Never use casts except you know what you are doing. Cast are not converting data!

convertedstring is a wide char string. With your example string the first letter is '1'. The ASCII code of that letter is 0x31. The Unicode (UTF-16, wide char code) of that letter is 0x0031. If you cast and assign the wide char pointer to a char pointer that will point to a char string with the first two bytes being 0x31 and 0x00. If you print that string it will just show the character '1' because the next char is NULL which is the string termination character.

AGAIN: If you need to display characters from multiple languages, use Unicode. Converting Unicode to multi byte will not work for characters not covered by the current code page.

That means: Your result variable (or output from your initial question) must be wide char.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900