Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countAbc challenge 9 49
mapShare challenge 13 68
Magic Software info 18 103
Windows Service to Receive TCP Packets 4 39
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 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 …

706 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