Jump to content

Recommended Posts

Posted (edited)

http://www.cplusplus.com/reference/string/basic_string/basic_string/

Specifically:

  Quote

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 by Shaggi

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

Posted

  On 2/19/2013 at 1:45 AM, 'Shaggi said:

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.

Posted

  On 2/19/2013 at 2:01 AM, 'JohnOne said:

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)

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

Posted

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:

wstring2utf8

You can also go with widen and narrow for individual characters.

My contributions:

  Reveal hidden contents

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 UDFsProcess 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)

Posted (edited)

  On 2/19/2013 at 2:26 AM, 'JohnOne said:

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 by Shaggi

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...