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

C++ locale and .NET -> conversion error using .NET

P: n/a
Hi all,

running the following code as an console application:

#include <locale>
#include <iostream>
#include <sstream>
#include <tchar.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
// ----------------------------------------
const char* arrLocaleNames[] = {
"German_germany",
"german-austrian",
"german-swiss",
"german-austrian",
"C",
"French",
"",
};
ostringstream* poStrStream = new ostringstream;
*poStrStream << "output of double a=1234.56E0 using different
locales:" << endl;
double a = 1234.56E0;
for (int i=0; i < sizeof(arrLocaleNames)/sizeof(arrLocaleNames[0]);
i++) {
poStrStream->imbue(locale( arrLocaleNames[i] ) );
*poStrStream << a << " <- locale(\"" << arrLocaleNames[i] << "\")
getloc().name():\"" <<
poStrStream- >getloc().name().c_str() << "\"" << endl;
}
// ----------------------------------------

cout << poStrStream->str();
delete poStrStream;

int k;
cin >k;
return 0;
}

leads to the correct output:

output of double a=1234.56E0 using different locales:
1.234,56 <- locale("German_germany")
getloc().name():"German_Germany.1252"
1.234,56 <- locale("german-austrian")
getloc().name():"German_Austria.1252"
1'234.56 <- locale("german-swiss")
getloc().name():"German_Switzerland.1252"
1.234,56 <- locale("german-austrian")
getloc().name():"German_Austria.1252"
1234.56 <- locale("C") getloc().name():"C"
1234,56 <- locale("French") getloc().name():"French_France.1252"
1.234,56 <- locale("") getloc().name():"German_Germany.1252"

But compiling the code between the two comment lines in a WinForms-project
as it is created by the VC2005-IDE using the same header files and namespace
std statement as above, results in the following output:

output of double a=1234.56E0 using different locales:
1234.56 <- locale("German_germany") getloc().name():"German_Germany.1252"
1234.56 <- locale("german-austrian") getloc().name():"German_Austria.1252"
1234.56 <- locale("german-swiss")
getloc().name():"German_Switzerland.1252"
1234.56 <- locale("german-austrian") getloc().name():"German_Austria.1252"
1234.56 <- locale("C") getloc().name():"C"
1234.56 <- locale("French") getloc().name():"French_France.1252"
1234.56 <- locale("") getloc().name():"German_Germany.1252"

Obviously the locale function produces the same output as in the console
version but the stream output stays with the standard "C" settings!

What do I have to change (compiler settings, move the piece of code out of
the click-handler of the WinForm) to get the correct results?

Thanks for your help

Rainer
Aug 22 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.