On Mar 23, 11:04 pm, inte...@interec .net wrote:
Ok, here is a sample program. $LANG is set to "en_US.UTF-8" on my
console. When I invoke this program from command line as:
$ ./a.out "आ१२३ॠªà¥«à¥¬"
argument argv[1] gets set to an array of multibyte characters in UTF-8
format. Correct?
The behaviour you're asking about is not defined by the C++ language.
You may wish to investigate, eg., comp.unix.progr ammer, or your
distribution's documentation about how it launches programs.
Then as a test, I convert argv[1] to a wstring as follows:
wstring sSql = make_wide(argv[1]);
and finally I print it out back to the console as:
wcout << sSql << endl;
The problem is that nothing is getting printed out on the console. I
tried using locale, but that didn't help either.
Have you tried examining the actual bytes being received in the argv
entries? Here's a short program (C++) that prints the character, hex
value, and decimal value of each character of each argv entry; try
running it with the above command line and seeing what it prints.
// Begin args.cpp
#include <iostream>
#include <iomanip>
void hexDump (const char *data, std::ostream &out) {
using std::endl;
using std::hex;
using std::dec;
for (int character = 0; data[character]; ++character) {
int asInt = data[character];
out << "Byte " << character << ": ";
out << hex << asInt;
out << " (" << dec << asInt << "): ";
out << data[character] << endl;
}
}
int main (int argc, char *argv[]) {
using std::cout;
using std::endl;
for (int argIndex = 0; argIndex < argc; ++argIndex) {
const char *argument = argv[argIndex];
cout << "Argument " << argIndex << ":" << endl;
hexDump (argument, cout);
}
}
// End of args.cpp
Then at least you will be aware of the encoded representation of the
command-line arguments. You may need to dike out out <<
data[character] part if the encoding is multibyte and any of the
individual bytes are interpreted as control characters; alternately,
<ot>pipe the output through less(1), which sanitizes its input before
displaying it</ot>.
#include <iostream>
#include <iomanip>
#include <locale>
#include <string// for std::wstring and std::string
>
using namespace std;
You should probably pick one of "using namespace std;" and using fully-
qualified names. Below you do both, whcih is somewhat confusing.
string make_narrow(con st wstring sArg);
wstring make_wide(const string sArg);
string make_narrow(con st wstring sArg)
{
std::string result;
std::locale loc;
// ...
Finally, please trim quoted text and reply inline, rather than quoting
the entire thread yet one more time and replying at the top. The
former is the accepted style for this newsgroup and the latter reads
"backwards" .