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

Problem with wctomb...

P: 2
Hi,
I'm trying to convert a wide character string in UTF-8 into a multibyte string using wctomb and I'm running into a problem when I try to convert characters that take more than one byte (ie, non ASCII characters). Below is simple code that produces the problem:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.   char  buffer[40];
  7.   for(size_t i=0; i<40; ++i)
  8.     buffer[i]=0;
  9. //  wchar_t wch = L'ア'; (UTF-8 65393)
  10.   wchar_t wch = L'';
  11.   wchar_t wide = 241;
  12.   cout << wch << endl;
  13.   int length;
  14.   if (wch == wide)
  15.     cout << "Yes!" << endl;
  16.  
  17.   length = wctomb( buffer, wch );
  18.   printf( "The number of bytes that comprise the multibyte "
  19.              "character is %i\n", length );
  20.   printf( "And the converted string is \"%s\"\n", buffer );
  21.  
  22.     return 0;
  23. }
If I substitue a simple character like L'e', then it works as expected, however with either of these more complicated characters (the first is Chinese, second is spanish) wctomb returns -1, meaning it couldn't convert the character. Why?
It fails if I use either the number or the actual letter representation, which makes sense because they are equivalent.

I'm running this on Suse Linux Enterprise Desktop 10 and compiling with g++ 4.1.0.

Thanks,
Andrew
May 31 '07 #1
Share this Question
Share on Google+
2 Replies


P: 2
I found the solution. In my shell I have the LANG environment variable set to en_US.UTF-8 so I thought everything would be okay. However, inside the program I added the following lines
Expand|Select|Wrap|Line Numbers
  1. #include <locale>
  2.     char* local = setlocale(LC_CTYPE, NULL);
  3.     cout << "Current LC_CTYPE is " << local << endl;
  4.  
and found out that it thought the LC_CTYPE is C. So adding the line
Expand|Select|Wrap|Line Numbers
  1.     setlocale(LC_ALL, "en_US.UTF-8");
made everything work as it should.
May 31 '07 #2

ilikepython
Expert 100+
P: 844
I found the solution. In my shell I have the LANG environment variable set to en_US.UTF-8 so I thought everything would be okay. However, inside the program I added the following lines
Expand|Select|Wrap|Line Numbers
  1. #include <locale>
  2.     char* local = setlocale(LC_CTYPE, NULL);
  3.     cout << "Current LC_CTYPE is " << local << endl;
  4.  
and found out that it thought the LC_CTYPE is C. So adding the line
Expand|Select|Wrap|Line Numbers
  1.     setlocale(LC_ALL, "en_US.UTF-8");
made everything work as it should.
Glad you solved your problem.
May 31 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.