Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2002-04-29
4
Medium Priority
?
1,066 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 1500 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
This is about my first experience with programming Arduino.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

604 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