JohnOne Posted February 19, 2013 Share Posted February 19, 2013 (edited) std::string to std::wstringIt works for me, but I've learnt that does not mean a thing in c++.string sSTRING("A string"); wstring wSTRING(sSTRING.begin(),sSTRING.end()); Edited February 19, 2013 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
jaberwacky Posted February 19, 2013 Share Posted February 19, 2013 I can't imagine a good reason for needing to do that. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
Shaggi Posted February 19, 2013 Share Posted February 19, 2013 (edited) http://www.cplusplus.com/reference/string/basic_string/basic_string/Specifically:The function template argument InputIterator shall be an input iterator type that points to elements of a type convertible to charT.where charT = wchar_t for std::wstrings.The casting and iteration process is safe per-se, but it's not guaranteed to be a valid ascii -> unicode conversion and as of such might output garbage.This operation is not reversable, as in, you cannot do this from unicode to ascii (it will truncate the wchar_t's to chars). Edited February 19, 2013 by Shaggi JohnOne 1 Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG Link to comment Share on other sites More sharing options...
JohnOne Posted February 19, 2013 Author Share Posted February 19, 2013 http://www.cplusplus.com/reference/string/basic_string/basic_string/ Specifically: where charT = wchar_t for std::wstrings. The casting and iteration process is safe per-se, but it's not guaranteed to be a valid ascii -> unicode conversion and as of such might output garbage. This operation is not reversable, as in, you cannot do this from unicode to ascii (it will truncate the wchar_t's to chars). Cheers. For the record the other way around from wstring to string also appears to work. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
JohnOne Posted February 19, 2013 Author Share Posted February 19, 2013 I can't imagine a good reason for needing to do that.Your dreams must be boring. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Shaggi Posted February 19, 2013 Share Posted February 19, 2013 Cheers. For the record the other way around from wstring to string also appears to work. Possibly if your wstring only contains characters from the ascii set, which in both standards are defined as the first 255. As soon as you get past that, you have troubles. eg. the letter 'Ā', in binary: 0000000100000000 will in a conversion done like this: wchar_t my_wc = 'Ā'; char my_c = (char)my_wc; be truncated to: 00000000 Or, null, which is the common string stop nominator (even though it works with std::strings .. you suddenly have unwanted null's in your string) JohnOne 1 Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG Link to comment Share on other sites More sharing options...
JohnOne Posted February 19, 2013 Author Share Posted February 19, 2013 Ah! yes, cheers, I always forget because I have never had a need to go beyond ascii. Must start taking these things into consideration. Thanks Shaggi. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Ascend4nt Posted February 19, 2013 Share Posted February 19, 2013 It's already been stated that going from wide to narrow strings is a bad idea, however if you are certain the characters are ascii or some ANSI subset, you can use a conversion function. Check out these C++11 conversion functions, from 'The Standard C++ Library, 2nd Ed':wstring2string (and vice versa)Also more appropriately, you can convert wide strings to UTF-8, and keep that in a std::string:wstring2utf8You can also go with widen and narrow for individual characters. JohnOne 1 My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code) Link to comment Share on other sites More sharing options...
Shaggi Posted February 19, 2013 Share Posted February 19, 2013 (edited) Ah! yes, cheers, I always forget because I have never had a need to go beyond ascii.Must start taking these things into consideration.Thanks Shaggi.No problem. But yes, keep in mind most conversions / copies in C++ are done bitwise and subject to conversion penalties (you may or may not notice for a long time ) unless you define another interface (this takes some getting used to when you're working with pointers and containers...)Anyway, the compiler should have emitted a warning when you did wstring -> string, depending on the code in the library.. If they did an explicit casting, it might not have done it. All those things that happen without you know it Edited February 19, 2013 by Shaggi JohnOne 1 Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now