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. 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.

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?  
Promote certifications in your email signature

Has your company recently won an award or achieved a certification? They'll no doubt want to show it off. Email signature images used to promote certifications & awards can instantly establish credibility with a recipient and provide you with numerous benefits.


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 seems that the first byte is only read from a double-byte TCHAR when creating a CString.
What do you think?


Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
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 how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

930 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