Solved

How do I declare a literal unicode string for the Mac

Posted on 2008-06-12
4
1,760 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 250 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

728 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