Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: wchar_t and printf not working



On Sun, 27 Mar 2005 09:30:24 +1000 Warwick Hall <email@hidden> wrote:

I have downloaded expat-1.95.8 (an XML cross platform C lib) and it has the option of using UTF-16, which is passed around in wchar_t* variables. I want to print these variables out to stdout, and i am not having any luck.

First of all, try running the following code. It should work, but you may need to re-configure Terminal for UTF-8 output.


#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main()
{
	setlocale(LC_CTYPE, "en_US");
	wchar_t buf[] = {1040, 1041, 1042, 0};

	printf("Test: %ls\n", buf);  // prints  "Test: АБВ"
	wprintf(L"Test: %ls\n", buf);  // same as above

	return 0;
}

Wide chars in C have no encoding associated with them, and are virtually obsolete (they were introduced before Unicode emerged). Setlocale can make wprintf work in platform-dependent and not particularly reliable ways, though.

Could it be that expat is assuming wchar_t is 2 bytes instead of the 4 bytes of darwin running on powerpc?

The size of wchar doesn't seem relevant in this case. However, the en_US locale uses UTF-32 on Darwin, and I do not know if it's possible to specify UTF-16 (en_US.UTF-16 doesn't work). Most UTF-16 codes are equally good UTF-32 ones, so ignoring the difference may work for you while testing - but for production code this won't do, of course.


- WBR, Alexey Proskuryakov
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-dev/email@hidden

This email sent to email@hidden
References: 
 >wchar_t and printf not working (From: Warwick Hall <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.