Solved

C++ and International characters

Posted on 2001-08-04
15
347 Views
Last Modified: 2013-12-03
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.
0
Comment
Question by:prashant_n_mhatre
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
15 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 6352193
You are using ASCII characters.  You need to switch to wide characters, like UNICODE characters, like

const wchar_t *x= L"Some accented or international characters";
cout << x << endl;
0
 
LVL 22

Expert Comment

by:nietod
ID: 6352200
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"
0
 
LVL 22

Expert Comment

by:nietod
ID: 6352204
>> 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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6352208
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
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6352211
EE site doesn't show my accented characters. Actually L"Aba" is an accented string.
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6352216
Ok...let's ignore command line option...but what about console application - like the above program?
0
 
LVL 22

Expert Comment

by:nietod
ID: 6352250
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.
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6352310
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...
0
 
LVL 22

Expert Comment

by:nietod
ID: 6352496
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.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6352510
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
0
 
LVL 22

Expert Comment

by:nietod
ID: 6352558
>> 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.
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6361628
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?
0
 
LVL 22

Expert Comment

by:nietod
ID: 6361668
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.
0
 
LVL 4

Author Comment

by:prashant_n_mhatre
ID: 6361699
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 !!!
0
 
LVL 22

Expert Comment

by:nietod
ID: 6361817
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question