C win32 api unicode to ascii [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do you convert LPCWSTR to const char *?
I have a function that gets a LPCWSTR string for win32 api usage.
Now I have a function that sends data in ASCII to the IRC server.
So now I need to convert it to ASCII, I haven't found any solutions, I only found ways to convert ASCII to UNICODE.

WideCharToMultiByte converts from UNICODE to MBCS. If your original wide char string consisted only of characters which could be represented in ASCII, then this will result in a ASCII string.
You can also use wcstombs which internally calls WideCharToMultiByte.
Either way, you will get a LPSTR or char * (which are the same).

Related

Is a C++ string class which I can use to process UTF8 strings? [duplicate]

This question already has answers here:
std::string and UTF-8 encoded unicode
(3 answers)
What encoding does std::string.c_str() use?
(2 answers)
Closed 8 years ago.
I think string can handle ANSI and wstring can handle Unicode16, is that right?
does C++ STL has class which can support UTF8 strings?
Just use std::string. It handles UTF-8 strings just fine.
Obviously you need to be aware that a codepoint can be 1 to 4 chars, and that a character can actually be any number of codepoints, but that rarely matters to you, and when it matters, std::wstring would have the same problems.
Big advantage is that std::string works the same everywhere. With std::wstring, different implementations use 16 bit or 32 bit numbers with very different meanings, there are problems with byte ordering and so on.

What does this string in c++ mean? [duplicate]

This question already has answers here:
What does the 'L' in front a string mean in C++?
(7 answers)
Closed 8 years ago.
LPWSTR l = L"D:/MyFile.txt";
I've searched everywhere, but couldnt find the answer. Thank you!
Long Pointer to Wide Character String. And you didn't search everywhere cause 1st google hit on LPWSTR points to MSDN:
The LPWSTR type is a 32-bit pointer to a string of 16-bit Unicode characters, which MAY be null-terminated. The LPWSTR type specifies a pointer to a sequence of Unicode characters, which MAY be terminated by a null character (usually referred to as "null-terminated Unicode").
typedef wchar_t* LPWSTR, *PWSTR;
And L in front of the literal specifies how the literal should be understood. It's kind of like suffixes for numeric types e.g: 10u, 4.0f. Because type is W (wchar_t), the appropriate counterpart for a literal definition is L.

WNDCLASSEX vs WNDCLASSEXW? [duplicate]

What is the difference in calling the Win32 API function that have an A character appended to the end as opposed to the W character.
I know it means ASCII and WIDE CHARACTER or Unicode, but what is the difference in the output or the input?
For example, If I call GetDefaultCommConfigA, will it fill my COMMCONFIG structure with ASCII strings instead of WCHAR strings? (Or vice-versa for GetDefaultCommConfigW)
In other words, how do I know what Encoding the string is in, ASCII or UNICODE, it must be by the version of the function I call A or W? Correct?
I have found this question, but I don't think it answers my question.
The A functions use Ansi (not ASCII) strings as input and output, and the W functions use Unicode string instead (UCS-2 on NT4 and earlier, UTF-16 on W2K and later). Refer to MSDN for more details.

Unicode in C++ support [duplicate]

This question already has answers here:
How well is Unicode supported in C++11?
(5 answers)
Closed 9 years ago.
There are some posts on this matter but I wanted to double check.
In Joel Spoolsky's article (link) one reads:
In C++ code we just declare strings as wchar_t ("wide char") instead
of char and use the wcs functions instead of the str functions (for
example wcscat and wcslen instead of strcat and strlen). To create a
literal UCS-2 string in C code you just put an L before it as so:
L"Hello".
My question is: Is what is written above, not enough to support Unicodes in a C++ app?
My confusions started when I couldn't output simple text like (in Russian):
wcout<<L"логин";
in console.
Also, recently I saw some code written for an embedded device where one person handles I think Unicode related strings using wchar_t.
Any help greatly appreciated.
This works in C++11 on a linux, utf8 machine:
#include <iostream>
int main(int, char**) {
std::cout << u8"Humberto Massa Guimarães\nлогин\n";
}
First, you can not print non-english characters in command-line
Second, briefly; UNICODE uses two bytes for every character and char uses single byte. For example string "ABC" will be stored in char as ABC\0 (3 bytes + end_of_string_character)
but in UNICODE will be stored as A\0B\0C\0\0\0 (6 + end_of_string_character which is two bytes like other characters)
For view some text, I suggest you to MessageBoxW:
First include windows header file: #include <windows.h>
Second use MessageBoxW API function:
MessageBoxW(0, L"UNICODE text body", L"title", MB_ICONINFORMATION);

convert SYSTEMTIME to LPCWSTR [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Is there a function like asctime_s() that works for SYSTEMTIME structures in windows?
I need to convert a type of SYSTEMTIME to a string. Then append the string product onto another string. Then finally convert the final string to type of LPCWSTR
All of the properties of the SYSTEMTIME are WORD types.
I think I need to convert the WORD types to wchar_t types and then concatenate them to get the string of SYSTEMTIME. How do I do this conversion and concatenation?
Once the final string has been built how can a LPCWSTR type be made from it?
Use GetTimeFormatEx or GetTimeFormat if you need to support Windows XP to format a SYSTEM time to a string.