Only showing one character while printing in C++ - c++

This is my code:
auto text = new wchar_t[WCHAR_MAX];
GetWindowTextW(hEdit, text, WCHAR_MAX);
SetWindowTextW(hWnd, text);
printf_s((const char *)text);
While printing, the char (text), it only outputs one character to the console.
It is a WINAPI gui and a console running together. It sets the winapi title successfully and get the text successfully, but i have no idea why this is only printing out one character to the console...

You're performing a raw cast from a wide string to a narrow string. This conversion is never safe.
Wide strings are stored as two-byte words in Windows. In your case, the high byte of the first character is 0, and x86 is little-endian, so the print stops at the first character.

Related

what's exactly the string of "^A" is?

I run my code on an online judgement. I log the string, key. Below is my code:
fprintf(stderr, "key=%s, and key.size()=%d\n", key.c_str(), key.size());
But the result is this:
key=^A, and key.size()=8
I want to what is the ^A represent in ascii. ^A's size is 2 rather than 8, but it shows that it is 8. I view the result by vim, and the log_file is encoded by UTF-8. Why?
Your viewer is electing to show you the bytes interpreted using a character encoding of its choosing and electing to show the resulting characters in caret notation.
Other viewers could make different choices on both counts or allow you to indicate what you want. For example, control picture characters (␁) instead of caret notation.
For a std:string c_str() is terminated by an additional \x00 byte following the actual value. You often use c_str() with functions that expect a string to be \x00 terminated. This applies to fprintf. In such cases, what's read ends just before the first \x00 seen.
You have several \x00 bytes in your string, which, of course, contributes to size() but fprintf will stop right at the first one (and not count it).
I have solve it by myself. If you write a std::string "\x01\x00\x00\x00\x00end" to a file and open it with vim later, you will get '^A'.
This is my test code:
string sss("\x01\x00\x00\x00\x00end");
ofstream of("of.txt");
for (int i=0; i<sss.size(); i++) {
of.put(sss[i]);
}
of.close();
After I open the file "of.txt", I saw "^A";

QTextBrowser not displaying non-english characters

I'm developing a Qt GUI application to parse out a custom windows binary file that stores unicode text using wchar_t (default UTF-16 encoding). I've constructed a QString using QString::fromWcharArray and passed it to QTextBrowser::insertPlainText like this
wchar_t *p = ; // pointer to a wchar_t string in the binary file
QString t = QString::fromWCharArray(p);
ui.logBrowser->insertPlainText(t);
The displayed text displays ASCII characters correctly, but non-ASCII characters are displayed as a rectangular box instead. I've followed the code in a debugger and p points to a valid wchar_t string and the constructed QString t is also a valid string matching the wchar_t string. The problem happens when printing it out on a QTextBrowser.
How do I fix this?
First of all read documentation. So depending on system you will have different encoding UCS-4 or UTF-16! What is the size of wchar_t?
Secondly there is alternative API: try QString::fromUtf16.
Finally what kind of character are you using? Hebrew/Cyrillic/Japanese/???. Are you sure those characters are supported by font you are using?

Windows.h - SetWindowText Shows the CR-LF Characters

I am writing a basic program in visual c++ that allows the user to enter text and then the program flips the text and displays it for the user to copy. The program works pretty good, until you add an enter to the EDIT box. When the user clicks to flip the text, instead of going down one line, it displays the actual characters for \r\n.
Is there a way to display the text as should instead of the actual string itself?
Here is how I set the text:
wchar_t lpwString[4096];
int length = GetWindowTextW(text->hwnd, lpwString, 4096);
SetWindowText(text->hwnd, flipText(lpwString, length));
Here is the method flipText
LPWSTR flipText(wchar_t textEntered[], const int len) {
wchar_t text[4096];
wchar_t flipped[4096];
wcsncpy_s(text, textEntered, len +1);
wcsncpy_s(flipped, textEntered, len +1);
for (int i = len -1, k = 0; i > -1; i--, k++)
flipped[k] = text[i];
return flipped;
}
"text" is just an object I created to store data for an EDIT box.
For an edit box, a return is a CR+LF sequence, when you reverse the text you are transforming it in an LF+CR, which is not recognized (it shows the individual characters). An easy way out could be to do a second pass on the reversed string and swap all the LF+CR pairs into CR+LF.
Incidentally, your flipText function is seriously broken - you are performing a useless extra copy of the original string, and you are returning a pointer to a local array, which is working only by chance. A way easier method could be just to reverse the string in-place.
Also, if you are working in C++ you should consider using std::string (or std::wstring if working with wide characters), which removes whole classes of buffers lifetime/size problems.
EDIT control needs '\r\n' combination to break. when you flip all the text, you get \n\r which means nothing to windows but text.
suggestion - flip the text and replace all the \n\r back to \r\n
Make sure ES_WANTRETURN style is used for Edit Box.
Also you should change \n\r back to \r\n right after flipText() call.

How to print wide character string?

The ISO-10646 code point value of Cyrillic lowercase a is 0x430, so I tried the following:
char u8str[] = u8"Cyrillic lowercase a is: \u0430.";
cout << u8str;
and
wchar_t wstr[] = L"Cyrillic lowercase a is: \u0430.";
wcout << wstr;
The Cyrillic lowercase a is successfully printed through u8str, but not wstr.
As for u8str, I've confirmed that its storage is initialized with the utf-8 encoding values of those characters (the Cyrillic lower case a occupies 2 bytes with the value D0 B0). All seems alright. The Cyrillic got printed correctly.
As for wstr, I suppose that each wchar_t in the wstr array is initialized with the numerical value of the encoding of the character in the execution wide-character set. While I don't fully understand what execution wide-character set is, I checked that the value of the Cyrillic lowercase a stored in the array is 0x430. Anyway, the Cyrillic letter doesn't get printed correctly. (Other characters are all OK.)
I'm a total novice for wchar_t stuffs, so my apology if this question is too elementary. What went wrong in my attempt printing the Cyrillic letter using wide character string? Is it an issue of the letter's representation in the execution wide-character set (what is this character set after all)? Or is it an issue about the incorrect usage of iostream facilities?
From: http://www.cplusplus.com/reference/iostream/cout/
A program should not mix output operations on cout with output operations on wcout (or with other wide-oriented output operations on stdout): Once an output operation has been performed on either, the standard output stream acquires an orientation (either narrow or wide) that can only be safely changed by calling freopen on stdout.
So only use one of the two.

String encoding VB6 / C++ dll

I am having a problem with some characters in 2 strings that my program uses.
String #1 is filled using VB code that gets data from a 3rd party application.
String #2 gets similar data from the same 3rd party application, but it gets it with a C++ dll and sends it to VB.
The data has some weird symbols in it.
I don't know a whole lot about encoding and different character sets, but I'll try to explain it the best I can.
I will use "Т" as my example character.
"Т" (note this isnt a normal capital t) it is unicode decimal value 1058
http://www.unicodemap.org/details/0x0422/index.html
When this character appears in String #1 during runtime it appears as "?", which I believe is just what VB6 does to show some unicode characters. When I use AscW on the character it returns the correct value of 1058.
When I output the string to a text file, it appears as "?".
The same character in String #2 from the C++ DLL appears as 2 characters "Т"
When I output that string to a text file, the character appears properly as "Т".
I was only outputting things to text files for testing purposes. I only need the 2 strings to be encoded / appear the same during run time.
Any idea whats going on here? Any way for me to get weird characters to appear the same in both strings?
Thanks
edit: also the C++ dll is in multi character set and sends the data in a BSTR string
CODE IN C++ DLL
allChat is a CString
BSTR Message;
int len = allChat.GetLength();
Message = SysAllocStringByteLen ((LPCTSTR)allChat,len+1);
Message is returned to the VB app.. and nothing happens to the string after that.
String #1 is just a regular VB string
From the way Cyrillic "T" becomes "Т", you get your string as a UTF8 encoded string (I verified that with Notepad++ by switching encodings). You need to convert it to Unicode before sending it to your VB app. Note that your VB app needs to be Unicode, not ASCII.
You can convert UTF8 to std::wstring with this function:
std::wstring utf8to16( const char* src )
{
vector<wchar_t> buffer;
buffer.resize(MultiByteToWideChar(CP_UTF8, 0, src, -1, 0, 0));
MultiByteToWideChar(CP_UTF8, 0, src, -1, &buffer[0], buffer.size());
return &buffer[0];
}