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

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

Posted on 2004-08-12
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
  • 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 250 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.
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.


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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

809 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