wide char

L

loudking

Dear all,

I am building a qt/c++ application and stuck on a wide char problem. Could anybody give me a hand? Thanks.


I have a print function that takes variable element argument and converted those arguments in a QString then calls QT native printing option. But output looks weird.

/**************
* Print
*/
void OutputWidget::print(wchar_t *fmt, ...)
{
QString line;
va_list arglist;
wchar_t wcharStr[5000];

va_start(arglist, fmt);
vswprintf(wcharStr, 4999, fmt, arglist);

line = QString::fromWCharArray(wcharStr);

print(line);
va_end(arglist);
}

/*******************
* Test Code
*/
wchar_t *fmt = L"abc=%S";
wchar_t *name = L"def";
QString line = QString::fromWCharArray(name);
outputWidget->print("fmt=");
outputWidget->print(L"fmt=");
outputWidget->print(QString::fromWCharArray(fmt));
outputWidget->print("name=");
outputWidget->print(line);
outputWidget->print(fmt, name);


#define BUF_SIZE 256
wchar_t wcsbuf[BUF_SIZE];
wchar_t wstring[] = L"ABCDE";
int num;

num = swprintf(wcsbuf, BUF_SIZE, L"%s", "xyz");
outputWidget->print(QString::fromWCharArray(wcsbuf));
num += swprintf(wcsbuf + num, BUF_SIZE - num, L"%S", wstring);
outputWidget->print(QString::fromWCharArray(wcsbuf));
num += swprintf(wcsbuf + num, BUF_SIZE - num, L"%i", 100);
outputWidget->print(QString::fromWCharArray(wcsbuf));
outputWidget->print(L"%d", 127L);

/****************
* Output
*/
fmt=
fmt=
abc=%S
name=
def
abc=d
祸z漱瑵異䑴物桃潯敳⤨洀楡ç®æ¹©æ½¤â¹·ç£ã©°ã¤·
祸z漱瑵異䑴物桃潯敳⤨洀楡ç®æ¹©æ½¤â¹·ç£ã©°ã¤·A
祸z漱瑵異䑴物桃潯敳⤨洀楡ç®æ¹©æ½¤â¹·ç£ã©°ã¤·A100
127


PLATFROM is Windows 7 64bit

/***********************
* Makefile flags
*/
CPPOPT = /EHsc -nologo -W3 -WX -DSTRICT -D_CRT_NONSTDC_NO_DEPRECATE -DUNICODE \
-MD -I.
CPPOPT += -DWIN64 -D_CRT_SECURE_NO_DEPRECATE -wd4244 -wd4267
 
Ö

Öö Tiib

... output looks weird.
....

num = swprintf(wcsbuf, BUF_SIZE, L"%s", "xyz");
outputWidget->print(QString::fromWCharArray(wcsbuf));
num += swprintf(wcsbuf + num, BUF_SIZE - num, L"%S", wstring);
outputWidget->print(QString::fromWCharArray(wcsbuf));

Problem is perhaps in those format specifiers "%S" and "%s".
To my knowledge in standard C++ 'wchar_t*' should have "%ls" and 'char*'
should have "%s" and "%S" does not exist. If what I said does not help then
read your compiler's documentation about format specifiers because
particular implementation may have it differently.
 
V

Victor Bazarov

Problem is perhaps in those format specifiers "%S" and "%s".
To my knowledge in standard C++ 'wchar_t*' should have "%ls" and 'char*'

The C++ Standard does not specify formats, to my knowledge. It relies
on the C Standard to do that because the printf function family is
inherited from C. That said, the reference I find online (e.g. see
http://www.cplusplus.com/reference/cstdio/printf/ ) does specify %ls for
wide string output.

The format specifier %S is a Microsoft thing, and it actually means "the
opposite", IOW it treats the argument as wide string in 'printf' and as
a single byte ("narrow") or a multibyte string in 'wprintf' functions.
See http://msdn.microsoft.com/en-us/library/hf4y5e3w.aspx for more
explanation.
should have "%s" and "%S" does not exist. If what I said does not help then
read your compiler's documentation about format specifiers because
particular implementation may have it differently.

V
 
L

loudking

On Thursday, April 17, 2014 4:55:42 PM UTC+8, Öö Tiib wrote:

Yes, problem is solved by changing %S to %ls. Thanks a lot!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top