469,628 Members | 1,031 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,628 developers. It's quick & easy.

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

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"
1á234,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
0 973

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Benoît Dejean | last post: by
3 posts views Thread by Ksenia Marasanova | last post: by
2 posts views Thread by Jorgen Grahn | last post: by
10 posts views Thread by Timothy Smith | last post: by
1 post views Thread by Cagdas Ozgenc | last post: by
3 posts views Thread by Torsten Bronger | last post: by
10 posts views Thread by David Thielen | last post: by
3 posts views Thread by robert | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.