Solved

C++ and International characters

Posted on 2001-08-04
15
305 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
  • 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now