Convert string to wstring windows




















Use the wstringstream class. This does NOT work for multi-byte character encodings. This is just a dumb way of throwing away type safety and expanding 7 bit characters from std::string into the lower 7 bits of each character of std:wstring.

This variant of it is my favourite in real life. It converts the input, if it is valid UTF-8, to the respective wstring. If the input is corrupted, the wstring is constructed out of the single bytes. This is extremely helpful if you cannot really be sure about the quality of your input data. You can use boost path or std path; which is a lot more easier.

This is from my sample code. Based upon my own testing On windows 8, vs mbstowcs can actually damage original string, it works only with ANSI code page. I have tested Vietnamese characters on finnish windows. But please notice that those macro's use heavily stack - don't use for loops or recursive loops for same function - after using W2A or A2W macro - better to return ASAP, so stack will be freed from temporary conversion. Here is my super basic solution that might not work for everyone.

But would work for a lot of people. It requires usage of the Guideline Support Library. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 11 years, 9 months ago.

Active 1 month ago. Viewed k times. Improve this question. Samir Samir 3, 7 7 gold badges 31 31 silver badges 40 40 bronze badges. Is it already encoded in UTF-8? If you enter those characters, it asks which encoding to convert the file to, and defaults to codepage Samir: more important is what is the encoding of the file? Can you move that string to the beginning of the file and show a hexdump of that part? We can probably identify it from that.

Show 1 more comment. Active Oldest Votes. There are just many for redundancy Improve this answer. Johann Gerell Johann Gerell Check what encoding you save VS files with — Johann Gerell.

Show 16 more comments. Pietro M Pietro M 1, 3 3 gold badges 19 19 silver badges 24 24 bronze badges. This only works if all the characters are single byte, i. Anything multi-byte will fail miserably, including UTF The question clearly contains multi-byte characters. This answer is clearly insufficient and does nothing but copy narrow characters as is into wide characters. See the other answers, particularly the one by Johann Gerell, for how to properly go from a multi-byte or utf8 encoded string to a utf16 wstring.

This answer is useful if you ignore the nuance of the question's body and focus on the question title, which is what brought me here from Google. As is, the question's title is extremely misleading and should be altered to reflect the true question being asked — Anne Quinn. Jones 1 1 gold badge 5 5 silver badges 7 7 bronze badges.

Add a comment. Active Oldest Votes. Improve this answer. Can you share the code? That looks nice. How would one do that?

Just make a template with specializations to convert between the various string types? AndiDog AndiDog 2 2 silver badges 3 3 bronze badges. I ended up adding the boolean parameter includeTerminator to both methods. Jamerson Jamerson 3 3 silver badges 9 9 bronze badges. Thank you.

You kind of down-play the problem by saying the approach is " somewhat controversial " - in my opinion, it's much more than that. It's wrong. I even have mentioned the "casting" word. Many thanks for reading in any case. Ferruccio Ferruccio 2 2 silver badges 6 6 bronze badges. For my usage these will typically be filenames. That will determine the correct code page to use. Jones: One way is to check if the string is valid UTF If not, assume it's ANSI.

Show 1 more comment. Roddy Roddy 6 6 bronze badges. Mark Loeser Mark Loeser 1, 1 1 gold badge 14 14 silver badges 15 15 bronze badges. Privacy policy. Codecvt The locale facet that represents the conversion object. If the conversion state object was not constructed with an explicit value, it is set to its default value the initial conversion state before the conversion begins.

Otherwise it is left unchanged. The number of input elements successfully converted is stored in the conversion count object. If no conversion error occurs, the member function returns the converted wide string. Otherwise, if the object was constructed with an initializer for the wide-string error message, the member function returns the wide-string error message object. The conversion state object that represents the state of the conversion.



0コメント