Click here to Skip to main content
Licence 
First Posted 17 Dec 2004
Views 20,672
Bookmarked 10 times

UNICODE is good for you, but ....

By | 17 Dec 2004 | Article
UNICODE to ANSI conversion

Unicode is cool. But converting from or to something else is &#@à'$£ cool :-(

Sometimes you need to convert from UNICODE to ANSI or ASCII or OEM or (add here what you want like UTF-x or Mac stuff) ....

So all functions we can use :

If you are reading in Strings overviews in MSDN you don't thing there is something else ...
The funny part is somewhere else ....

In a little program with all UNICODE stuff ready try this:

    LPCWSTR p = L"hello world éèçàêôî"; // this char are from my french keyboard (sorry I'm using a french keyboard) <BR><BR>    char result[MAX_PATH];<BR>    CharToOem( p, result );<BR><BR>    char result2[MAX_PATH];<BR>    wsprintfA( result2, "%S", p );

And the result is :
result1 => "hello world ‚Ї…ˆ“Œ"
result2 => "hello world éèçàêôî"

Well it's not the same, I thing wsprintfA %S doesn't use CharToOem it's obvious ...
There is someting else somewhere ....
try Unicode and Character Set Functions

Bingo, WideCharToMultiByte seem attractive. Let's try .....
    char result3[MAX_PATH];<BR>    WideCharToMultiByte( CP_ACP, 0, p, -1, result3, MAX_PATH, NULL, NULL );<BR>
Yes that's it, result3 => "hello world éèçàêôî"

Conclusion :
OEM and ANSI are not the same and windows documentation is not always very clear :-)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

ejor

Web Developer

United Kingdom United Kingdom

Member

http://dev.jesover.net

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralPlease kick this "article"! PinmemberAlexander M.7:13 18 Dec '04  
GeneralI feel like frank grimes Pinmembernorm.net5:00 17 Dec '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 17 Dec 2004
Article Copyright 2004 by ejor
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid