?
Solved

How do I declare a literal unicode string for the Mac

Posted on 2008-06-12
4
Medium Priority
?
1,762 Views
Last Modified: 2010-05-18
How do I declare a literal unicode string?  For example, with Windows I would simply use:

WCHAR text[]      = L"This is a unicode string";

For the Mac, I would like to do something like

UniChar text[] = L"This is a unicode string";

Now I know about CFSTR and CFStringRef, and that is not what I am looking for.  Is there a way to declare non-encapsulated literal unicode strings?
0
Comment
Question by:JohnGaby
[X]
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
  • 2
  • 2
4 Comments
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 1000 total points
ID: 21776186
The c++ way (and therefore cross platform way) to declare a wide character string is thus: -

wchar_t text[] = L"This is a unicode string";


Please note, don't confuse Unicode (which is a character encoding scheme) with the ability to store Unicode. C++ knows nothing of Unicode, it just knows about narrow and wide chars (it is the OS that provides the ability to encode/decode Unicode). Declaring a wide string doesn't mean the contents are Unicode, it just means you have a storage vessel capable of storing Unicode in the appropriate Unicode Transformation Format. You can store Unicode in 8, 16 and 32 bit storage types, put this another way, you can store Unicode in a simple char type. There are 3 standard was of encoding Unicode, there are UTF8, UTF16 and UTF32. UTF8 is an 8 bit multi-byte encoding, UTF16 is a 16 bit multibyte encoding and UTF32 is a 32 bit fixed byte encoding. The one you choose depends upon the size of your storage vessel. For example, on Linux a wchar_t is a 32 bit type whereas on Windows it is a 16 bit type. Therefore on Linux the native encoding type of Unicode is normally UTF32, whereas on Windows it is UTF16. It is important to know this if you are using Unicode otherwise things may become a little confusing.

There are some good pages on Wikipedia that explain all about Unicode, UTF8/16/32.
0
 
LVL 9

Author Comment

by:JohnGaby
ID: 21780295
I am quite aware of the different flavors of unicode, and know that what is used it is OS dependent.  That is why I am asking questions about the Mac.  On the Mac, the basic string type is CFString, correct?  Now I can create this using a UTF8 string, or a UTF16 string (UniChar), but I don't see any way to create them using a UTF32 string.

I am a Windows programmer, so I am used to having my whole program use UTF16 strings everywhere.  I am also used to being able to index these strings, which I cannot do with CFString(?).  What I was wondering if there was a way to declare literal UTF16 (UniChar) strings for the Mac, or am I forced to use CFString everywhere?

OS X is Unix, so of course the type wchar_t is 32 bit, and therefore does not seem to be very useful in this context.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21780761
>> I am quite aware of the different flavors of unicode
Good, just wanted to be sure we're all on the same page -- it seems we are :)

>> OS X is Unix, so of course the type wchar_t is 32 bit, and therefore does not seem to be very useful in this context.
We handle all strings internally as UTF8 as it's the simplest way to provide cross-platform portability. We convert, if necessary, at the interface boundary.

Sorry, I don't know Macs so I can't advise specifically.
0
 
LVL 9

Accepted Solution

by:
JohnGaby earned 0 total points
ID: 21788953
Well, for anyone else looking for an answer to this question, it looks like the short answer is "You can't".  For the Mac OS X the way you handle unicode strings is through the use of the CFString functions.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Preface   In the first 100 days of the Macintosh release in January 1984, Apple sold 72,000 of the computers. Relatively cheap, easy-to-use personal computer with a graphical operating system. I've never seen this model. I did see this one:   …
Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

764 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