473,324 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Problem with wctomb...

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
2 2898
allez
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
844 Expert 512MB
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

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

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
117
by: Peter Olcott | last post by:
www.halting-problem.com
16
by: Daniel Rudy | last post by:
....is that there is no single man page that lists the stdlib functions that I can reference. I'm working in a Unix environment. -- Daniel Rudy Email address has been encoded to reduce spam....
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
9
by: Ross | last post by:
Hi, I have a question regarding how the mbtowc() and wctomb() functions work. Given that some compilers (gcc, for example) allow the wide execution character set to be specified at compile time,...
3
by: lorenzvvv | last post by:
Hello! I need to compile a program where wctomb and mbtowc functions are in. These functions are useful for conversions from wide characters to multi-byte and vice versa. Avr-gcc lacks wctype.h...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.