Link to home
Start Free TrialLog in
Avatar of prashant_n_mhatre
prashant_n_mhatre

asked on

C++ and International characters

How can I use

char *x="Some accented or international characters";
cout << x << endl;

In VC++ on console window it shows junk.

also I would like to accept accented characters as input from command line. (I know argc, argv and getopt)

Sample code will be helpful.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

Note that I madee a few changes there.  First of allj, techically string literals are constants in C++.  This is true for both ASCII and widw character string literals.  Many compilers will still allow you to treat them as non-constant, but this is still a mistake.  And if you change this literals your will cause problems  Pluss someday the compiler will probably be updated and no longer allow it.  So pavoid problems and treat them as constants like

const char *somestring = "some text";

next, if you want to create a character or sting literal that is a wide character, use a "L" before the literal, like

wchar_t widechar = L"A";
wchar_t astring[10] = L"ABC"
>> I would like to accept accented
>> characters as input from command
>> line. (I know argc, argv and getopt)
that appears to be impossible in windows.

You can use GetCommandLineW(), the unicode version of GetCommandline().  This will return a unicode version of the command line.  However, I have been told that it does not handle non-ASCII characters, probably due to the DOS roots of the command system.  

You might try asking about this on the windows topic area, since this is a windows issue, not a C++ issue, but I don't think you will find any option.
Avatar of prashant_n_mhatre

ASKER

Doesn't work. I tried following:

#include  <iostream >
using namespace std;

int main( void )
{
   const  wchar_t *x= L"Aba";
   cout << x << endl;
   return 0;
}

It gives:

0046B01C

as output. I'm using VC++ 6.0
EE site doesn't show my accented characters. Actually L"Aba" is an accented string.
Ok...let's ignore command line option...but what about console application - like the above program?
Opps.  I forgot the fact that you also need to output this.  The part I showed you was to define a wide character string.  But then to output it you have to use a wide character stream object, like wcout.  Like

const wchar_t *x= L"Some accented or international characters";
wcout << x << endl;

similarly there is a wide character input stream, wcin.
still it doesn't give the correct output. It shows..
??b??

I guess we may need to set something that will give hint to the compiler that the next output stream is a wide character stream...
That should work fine.  It works for me on VC at least.

Note that for non-ASCII characters you will need to enter the proper unicode character.  If you are entering an extended ASCII character (from decomal 128 to 255) these characters might not coorespond to the unicode character you want.
Avatar of DanRollins
prashant_n_mhatre,
The problem is quite likely related to the font that is available when you output to the console.  
* In which language are you attempting to output?  
* Which font have you chosen for use in your console windows?  
* Which version of Windows are your running?  
* Is windows running with a default language of Enginsh?

Hi nietod,
The experts in this section have agreed to NOT lock questions.  That is, we all post Comments so that
the question remains visible to other experts.

It would seem like a good point-making strategy to lock questions immediately (in your case, less than
one hour after the post), but it deprives the question Asker of the additional expert input that might
solve his specific problem.  After several days of give-and-take, when the "real" underlying question
has been revealed, if your post is quite clearly the only possible correct solution, it may (*may*)
be appropriate to post an Answer.

In the future, please post Comments, like everybody else.  Thanks!

-- Dan
>> The experts in this section have agreed
>> to NOT lock questions.
That is absolutely untrue.

>>  the future, please post Comments,
>> like everybody else.  Thanks!
In the future, please notify customer service when you have a complaint.
nietod:

I'm also using VC. Is there something to do in control panel? My language is set to 'English(United States)'.

If different font is required..what shall I download?
teh part I've answered is how to make a standard C++ program output wide character strings.  That is a standard C++ issue.  When it comes with how to handle how to make windows handle wide characters in a console window (which is not a c++ issue, it is a windows issue) I am mostly at a loss.

You might look into the windows API functions like SetconsoleCP() which sets the code page used to ditermine charactes that appear on the console window.  This part is not my area of expertise.

If necessary you might have to reject my answer to see if another expert can help.  However, your best bet is to ask this question on the windows topic area.  The C++ part has been solved, you now need to know how to make a console window show unicode characters, that is a windows question.
There is no question of rejecting your answer. I'm looking for standard C++ only. Although I use VC++ to test sample programs, I work on Unix.

I have found a good starting point.

Thanks a lot !!!
I'm not sure what support UNIX has for wide characters and uncode.  UNIX is much older than these ideas.  However modern versions probably have support.  You may have to do things to set the code page or similar type things.  I'm not sure how standardized it will be though.  i.e. it may depend a lot on what brand of UNIX you are using.