Jump to content

This conversion safe? (c++) (Solved)


JohnOne
 Share

Recommended Posts

std::string to std::wstring

It 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 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

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

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

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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