Solved

Converting a unicode string to a mbcs (multi byte character) string

Posted on 2002-04-29
4
1,050 Views
Last Modified: 2012-08-13
Ok I am having no luck on a Japanese machine using the following code to convert a unicode string to a mbcs :

size_t nSize = wcstombs( NULL, pszWide, NULL );

if( nSize != (size_t)-1 )
{
  m_pszShort = new char[nSize+1];

  m_pszShort[nSize] = '\0';

  size_t nCopied = wcstombs( m_pszShort, pszWide, nSize );
}

When I try to convert a legitimate Unicode character under the OS locale, it always returns -1.  I've
am absolutely positive that the single unicode japanese character in the pszWide string is valid (if
I save the text file as html it replaces it with the decimal equiv, which I verified is the value in
pszWide).

Is the wcstombs function broke?  Or is there something I am missing?
0
Comment
Question by:dwinkler
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 1

Accepted Solution

by:
snoopym earned 500 total points
ID: 6980147
The msdn help for wcstombs sais that the conversion depends on the current defined locale.
try using the function setlocale and setmbcp before  calling wcstombs.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_locale.asp

notice the note in that url:
For multibyte routines, the multibyte code page must be equivalent to the locale set with setlocale. _setmbcp, with an argument of _MB_CP_LOCALE makes the multibyte code page the same as the setlocale code page.

Another possibility is calling WideCharToMultiByte.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/intl/unicode_2bj9.asp
0
 

Author Comment

by:dwinkler
ID: 6981734
Figured it out (kind of).  Points go to the person who can answer the new questions.  You must call setlocale(...) in each exe and dll.  Does anyone know why?  Shouldn't the dll and exe be in the same address space with the same global variables?  So you can either place the setlocale in either a function of the dll that is called or in a callback from the exe:

i.e. if you want to call sprintf( mbcsbuf, "%ls", widebuf ); inside of the dll and get the correct result.

A - application
B - dll

A->setlocale( LC_ALL, "" ); (does not work)

A->B->setlocale( LC_ALL, "" ); (works)

A->B->A->setlocale( LC_ALL, "" ); (works)

How does it switch context between the dll globals and the exes?
0
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 7005298
First of all, wcstombs is a C runtime library function. It is therefore limited by the same rules as the RTL. Therefore, if you have an application and a DLL and you want them to act as if they were compiled together, then you need to make sure that both the EXE and the DLL were compiled with the same compiler with the same compiler options AND you need to make sure they were built the SHARED version of the RTL. See the online help with /MD (if you are using Visual C++).
0
 
LVL 54

Expert Comment

by:nico5038
ID: 7515075

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
 - PAQ'd and pts removed
Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

Nic;o)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Progress

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question