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


How do I define Unicode support for just a small segmant of code?

Posted on 2004-08-12
Medium Priority
Last Modified: 2010-04-15
How do I define Unicode support just for a small segmant of code?

I have an array of type char.  When the array is filled with single byte characters and then later output the contents to the console all appears fine (human readable).  

But what if the array is filled with Unicode (double byte) characters...the output is garbage...ok...this obviosly makes sense.  So.....is there then a way to output the correct Unicode character onto the screen?

What I"m trying to do is avoid changing the whole application to support Unicode for just that tiny tiny segmant.

Question by:zarrona
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
  • 4

Expert Comment

ID: 11803118
You have a couple of options. If you are using TCHAR for your strings, you can define unicode inline:

some code
#define _UNICODE

code that required unicode characters

#undef _UNICODE

more code

You can also specifically call the unicode version of a function. For instance, sprintf() has several
  sprintf() is the ASCII version while swprintf() is the wide (unicode) version.
  strlen() is the ASCII version while wcslen() is the unicode version.


Author Comment

ID: 11815447

Doesn't seem to work.....

All TCHAR defined between the _UNICODE definitions seem to be 1 byte rather than 2 bytes.

Must there be something else I'm missing?


Accepted Solution

jimwasson earned 750 total points
ID: 11815712
Hmmm. Looks like I led you astray. Sorry.

You can use WCHAR in place of TCHAR where you want unicode characters and strings, i.e.,

TCHAR tch[] = _T("Hello there"); // tch is a char array.
WCHAR tch1[] = L"Hello there";   // tch1 is a unicode (wide char) array.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 11816772

Here is the situation....
I have a file named    Ωωθε.rtf     (Notice Greek symbols are used)
//unicode translation Ox03A9 Ox03C9 Ox03B8 Ox03B5 Ox002E Ox0072 Ox0074 Ox0066

    char nameBuffer[256];
//I then read the file name into a char buffer
    int bytesReadFromFile = read (FilesetContainerFile, &nameBuffer, htonl(oneFile.fNameLength));
//bytesReadFromFile = 16   ....sounds good  8 characters multiplied by 2 bytes each = 16
//oddly while debug nameBuffer appear in memory as          .©.É.¸.µ...r.t.f  
//unicode translation Ox00A9 Ox00C9 Ox00B8 Ox00B5 Ox002E Ox0072 Ox0074 Ox0066
//oddly it seems that every other byte is skipped and translated

How do I know create a CString that translates the buffer into it's Unicode equivelant?  

Expert Comment

ID: 11820718
CString? What exactly are you using for CStrings?

Author Comment

ID: 11822808
I need to pass a CString with the file name (in unicode) to an outside module.

I had set my project settings to "Use MFC in a Shared DLL"....that way I can use CStrings.
I have found a way to build a CString but it's not so correct.

CString myName;
for (unsigned int j=0; j<strlen(nameBuffer); j +2)
      myName += char(nameBuffer[j+1]);

Anyhow....I need to get _UNICODE defined in code for this area but I'm not able to get it working....All characters between _UNICODE defintions.  That way I can use TCHAR or WCHAR.


Expert Comment

ID: 11823154
This looks complicated.

To set the CString from your buffer you can just do this:
CString myName = nameBuffer;

It seems to mix Unicode and MBCS you have to handle the exception on a case-by-case basis.

Author Comment

ID: 11826989
here is a code snippet....

#define _UNICODE

TCHAR unameBuffer[256];
bytesReadFromFile = read (FilesetContainerFile, &unameBuffer, htonl(oneFile.fNameLength));  //byteReadFromFile = 16...good

CString myName, hold;
TCHAR holdName;
for (unsigned int j=0; j<bytesReadFromFile/2; j ++)
      holdName = htons((TCHAR)(unameBuffer[j]));  //holdName= 0x03A9  (Omaga character)...good so far
      CString hold(holdName);                                 //hold = "©|" = 0xA903
      myName += hold;                                          //myName = "©É¸µ.rtf" at end of for-loop
                                                      //NOT 0x03A9 (Omega) 0x03C9 (Omega) 0x03B8 (Theta) 0x03B5 (Epsilon).rtf

#undef _UNICODE

Close but not quite....it seems that the first byte is only read from a double-byte TCHAR when creating a CString.
What do you think?


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

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