Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

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

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.

Thanks
0
zarrona
Asked:
zarrona
  • 4
  • 4
1 Solution
 
jimwassonCommented:
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
versions:
  sprintf() is the ASCII version while swprintf() is the wide (unicode) version.
  strlen() is the ASCII version while wcslen() is the unicode version.

0
 
zarronaAuthor Commented:
Hi,

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?

thanks
0
 
jimwassonCommented:
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.
0
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!

 
zarronaAuthor Commented:
ok...

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?  
           
thanks
0
 
jimwassonCommented:
CString? What exactly are you using for CStrings?
0
 
zarronaAuthor Commented:
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;
myName.Empty();
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.

thanks
0
 
jimwassonCommented:
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.
0
 
zarronaAuthor Commented:
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;
      
myName.Empty();          
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?

thanks
0

Featured Post

Independent Software Vendors: 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!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now