By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

ascii "" and unicode L"" question

P: n/a
mr

How can i 'force' c++ to interprete "blabla" strings as unicode string
instead of ascii string (i just don't want to add 'L' before the thousands
strings that are on my projects...), as all my projects are using unicode,
and i don't see any reason that c++ compilator keep creating ascii

Is it a way?

thanks !
Jul 5 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* mr:
How can i 'force' c++ to interprete "blabla" strings as unicode string
instead of ascii string (i just don't want to add 'L' before the thousands
strings that are on my projects...), as all my projects are using unicode,
and i don't see any reason that c++ compilator keep creating ascii

Is it a way?
You can not force Unicode in any portable way in C++.

You can change all your string literals to wide string literals by
running a script or C++ program that prepends L to every string literal.

But in addition you need to replace char with wchar_t, std::string with
std::wstring, and so on, and if you have calls to library functions that
expect char-based arguments you'll need to fix those calls to (I suggest
leaving exceptions as char-based because Unicode exceptions have no
advantages that I know of, and many drawbacks).

Also, at least one popular compiler doesn't support std::wcout & family,
so you'll have to use some workaround (this includes a decision about
how to handle i/o).

Good luck: it's a bit of work, but it can pay off.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 5 '06 #2

P: n/a

mr wrote:
How can i 'force' c++ to interprete "blabla" strings as unicode string
instead of ascii string (i just don't want to add 'L' before the thousands
strings that are on my projects...), as all my projects are using unicode,
and i don't see any reason that c++ compilator keep creating ascii

Is it a way?

thanks !
I would have to agree with the other poster, adding the L before string
and character literals is the least of your problems, but a good place
to start.

I have already done this for a huge project and it is a lot of work.
You need to examine every single place you use strings and determine
what the correct types are. Some places you must still use narrow
strings (such as network protocols and most file IO). In other places
the format of the wide and narrow strings will depend on other factors
and you will need to write code to narrow and widen strings correctly
taking these factors into account.

To correctly support Unicode is more than just turning on wide
character support though. std::wstring is not able to handle Unicode
strings correctly (not any better than std::string can).

How much work you need to do depends a lot on what the application does
and is one of the reasons why you will need to examine every use of a
string or character (remember for example that wchar_t on most
platforms is not big enough for one Unicode character - you need 32
bits, and that's assuming you can avoid any issues of
canonicalisation).

My tip is to start with all the code that performs IO and then look at
any code that makes use of third party APIs. Thin wrappers for all of
these places are often a good way to handle all of this.

Jul 5 '06 #3

P: n/a
mr posted:
>
How can i 'force' c++ to interprete "blabla" strings as unicode
string instead of ascii string (i just don't want to add 'L' before
the thousands strings that are on my projects...), as all my projects
are using unicode, and i don't see any reason that c++ compilator keep
creating ascii

Is it a way?

What I do is define the following:

#ifdef WIDE
#define LITERAL(x) L##x
#else
#define LITERAL(x) x
#endif
And write code as follows:

SomeFunc( 5, 3, LITERAL("Version 7"), 3 );

--

Frederick Gotham
Jul 5 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.