I wrote a large project dealing with images, files, and WINAPI functions.

I decided to add RichTextEdit to it and used the msfedit.dll.. Turns out that it only supports UNICODE style strings and chars and my entire project is std::strings and LPCSTR's, etc..

None of the WINAPI functions are unicode either..

I'm thinking of converting the whole project to unicode so I won't face such a problem further along the line but I don't know where to start because it's a mixture of everything!

Bitmaps use chars and unsigned chars.. I can't think of a way to read a bitmap using wchar_t.

Things like ZLib and LibPng use FILE*, gzopen, and fopen, and base64 chars.. how would I work around that?

Reading with fstream vs wfstream and printing with cout vs wcout.. Is there any typedefs to enable either or? Just incase I decide I don't like Unicode? What happens if I read a file using wfstream, will it be the same as fstream but just able to support more characters?

I made a file like below:

#ifndef UNICODE_HPP_INCLUDED
#define UNICODE_HPP_INCLUDED

#include <tchar.h>
#include <iostream>

#define UNICODE
#define _UNICODE

#ifdef UNICODE
    typedef std::wstring string_type;
    typedef wchar_t char_type;
    typedef std::wfstream fstream_type;
    typedef std::wstreambuf streambuf_type;
#else
    typedef std::string string_type;
    typedef char char_type;
    typedef std::fstream fstream_type;
    typedef std::streambuf streambuf_type
#endif

#endif // UNICODE_HPP_INCLUDED

Would I need anything else? Will this break my project? What's the best approach to support Unicode and ANSI?

Recommended Answers

All 3 Replies

UNICODE only affects character strings, not bitmaps. For example, you need to convert the string literal "Hello World" by enclosing it in a macro _T("Hello World") (defined in tchar.h). You could also do it by putting capital L in front, such as L"Hello World". Using the _T or _TEXT macro (they are both the same result) is preferable because you can then compile the program with or without UNICODE and not change the code. If you use the L the string will always be UNICODE regardless of how the program is compiled.

Just because you compile a program for UNICODE doesn't automatically change the contents of any files. If your program is attempting to read a file that was written with UNICODE characters then the program will need to read it with wfstream. If the file was not written with UNICODE characters then the program will need to read the file with fstream and convert any strings (not bitmaps) to UNICODE before passing to functions that take UNICODE strings as arguments. There are functions that convert between UNICODE and non-UNICODE strings, or you could write your own function fairly easily if the language is English. In any event, attempting to typecase from char to wchar_t (or vice versa) does not work.

Hmm. It affected my bitmaps.. I had to change all the bitmap code back to chars because I changed them to wchar_t and it read the bitmaps wrong.. Reads it correctly with regular chars.

Anything to do with buffers, I messed up and had to change to normal. That includes serialization and sockets :(

Other than that, I got almost everything working! Thanks.

I had to change all the bitmap code back to chars because I changed them to wchar_t and it read the bitmaps wrong

Yup -- sizeof(wchar_t) is not the same as sizeof(char). UNICODE doesn't have any affect on bitmaps or other types of binary data. It only affects human-readable strings.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.