Solved

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

Posted on 2002-04-29
4
1,021 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
oracle query help 18 99
c++ reading data from file into two dimensional array 3 96
Basic Java Case or If-Else statement... 3 43
Advice in Xamarin 21 56
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about a problem I had getting the GPS LocationListener working.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now