473,799 Members | 3,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Unicode in C programs

I'd like to start using Unicod (especially UTF-8) in my C programs, and
would like some infos on how to start.
Can you tell me some documents (possibily online) explaining Unidoce
and UTF-8, and how I can use them in my programs (writing and reading
from file, from the console, processing Unicode strings and chars
inside the program, etc...)?

Thanx

Nov 15 '05 #1
4 6218
Marco Iannaccone wrote:
I'd like to start using Unicod (especially UTF-8) in my C programs, and
would like some infos on how to start.
Can you tell me some documents (possibily online) explaining Unidoce
and UTF-8, and how I can use them in my programs (writing and reading
from file, from the console, processing Unicode strings and chars
inside the program, etc...)?


C provides a concept of wide characters (arrays of wchar_t) and
multibyte characters (arrays of char where each character may take up
more than one byte). The C standard defines functions for converting
between wide and multibyte representations . The standard does not
specify what encoding these two representationa l forms take.

On at least one platform, depending on the current locale setting, the
wide characters built in to C represent Unicode characters, and the
multibyte characters represent the UTF-8 form.

The following program attempts to set the locale to en_AU.UTF-8, which
means Australian English in UTF-8 encoding. The language portion doesn't
matter, just the encoding does. It then takes a UTF-8 string (which
happens to contain Simplified Chinese characters), and converts it to
the wide character representation, which on my platform is equivalent to
Unicode.

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

int main(void)
{
wchar_t ucs2[5];
if(!setlocale(L C_ALL, "en_AU.UTF-8"))
{
printf("Unable to set locale to Australian English in UTF-8\n");
return 0;
}

/* The UTF-8 representation of string "æ°´è°ƒæ*Œå ¤´"
(four Chinese characters pronounced shui3 diao4 ge1 tou2) */
char *utf8 = "\xE6\xB0\xB4\x E8\xB0\x83\xE6\ xAD\x8C\xE5\xA4 \xB4";

mbstowcs(ucs2, utf8, sizeof ucs2 / sizeof *ucs2);

printf("UTF-8: ");
for(char *p = utf8; *p; p++)
printf("%02X ", (unsigned)(unsi gned char)*p);
printf("\n");

printf("Unicode : ");
for(wchar_t *p = ucs2; *p; p++)
printf("U+%04lX ", (unsigned long) *p);
printf("\n");

return 0;
}

[sbiber@eagle c]$ c99 -Wall utf8ucs2.c -o utf8ucs2
[sbiber@eagle c]$ ./utf8ucs2
UTF-8: E6 B0 B4 E8 B0 83 E6 AD 8C E5 A4 B4
Unicode: U+6C34 U+8C03 U+6B4C U+5934

I'd be interested to know how widespread this technique works. Is it
portable?

--
Simon.
Nov 15 '05 #2
"Marco Iannaccone" <m.**********@g mail.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
I'd like to start using Unicod (especially UTF-8) in my C programs, and
would like some infos on how to start.
Can you tell me some documents (possibily online) explaining Unidoce
and UTF-8, and how I can use them in my programs (writing and reading
from file, from the console, processing Unicode strings and chars
inside the program, etc...)?


The best and the most authorative source of info on all aspects of Unicode
is www.unicode.org.
At least read the Unicode FAQ and the article on Unicode "To the BMP and
beyond!" by Eric Muller of Adobe Systems (the doc must be linked somewhere
at unicode.org -- or just google for it). Read that info with attention.
By default, Unicode isn't guaranteed to be supported by anything in every
compiler on every system, unlike ASCII. But, to the best of my knowledge
recent linux distros support UTF-8 in functions like printf() and fopen().
Once again, make use of www.unicode.org.

Alex
Nov 15 '05 #3
On Thu, 01 Sep 2005 02:53:57 -0700, Marco Iannaccone wrote:
I'd like to start using Unicod (especially UTF-8) in my C programs, and
would like some infos on how to start.
Can you tell me some documents (possibily online) explaining Unidoce
and UTF-8, and how I can use them in my programs (writing and reading
from file, from the console, processing Unicode strings and chars
inside the program, etc...)?


If you would like a quick intro into what your up against see this link:

http://www.io plex.com/~miallen/libmba/dl/docs/ref/text_details.ht ml

It describes an api used to improve portability of code across different
platforms which you may or may not be concerned with but it does describe
the basics of working with Unicode in C.

Mike

Nov 15 '05 #4
Thanx a lot! :-) (and thanx to everyone for helping! I'll start
studying...! :-P)

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2787
by: yzzzzz | last post by:
Hi, I am writing my python programs using a Unicode text editor. The files are encoded in UTF-8. Python's default encoding seems to be Latin 1 (ISO-8859-1) or maybe Windows-1252 (CP1252) which aren't compatible with UTF-8. For example, if I type print "é", it prints é. If I use a unicode string: a=u"é" and if I choose to encode it in UTF-8, I get 4 Latin 1 characters, which makes sense if the interpreter thinks I typed in u"é".
6
18334
by: Spamtrap | last post by:
I only work in Perl occasionaly, and have been searching for a solution for a conversion, and everything I found seems much too complex. All I need to do is take a simple text file and copy it, however some specific lines are in fact in UTF8 as printed garbagy characters and they need to be converted to Unicode, so that the new text file can be imported into a desktop program and into some Word documents. For the moment I would be...
1
2096
by: krammer | last post by:
Hello, Can any one please give me a short but concise pros and cons list of Unicode support in both SGML and XML? long story short, we are gonna port our leagacy SGML files to XML and the new XML files will have foreign (CJK) and Ascii/English in them. XML would be better to store the text in cuase it has better Unicode support than SGML right???? what are these advantages that XML has
4
3172
by: Basil | last post by:
Hello. I have compiler BC Builder 6.0. I have an example: #include <strstrea.h> int main () { wchar_t ff = {' s','d ', 'f', 'g', 't'};
4
5253
by: LinasB | last post by:
Hi, How to read system setting value of "Language for non-Unicode programs" ? Or how to set it programmatically? LinasB
61
4964
by: Christoph Zwerschke | last post by:
On the page http://wiki.python.org/moin/Python3%2e0Suggestions I noticed an interesting suggestion: "These operators ≤ ≥ ≠ should be added to the language having the following meaning: <= >= != this should improve readibility (and make language more accessible to beginners).
3
29728
by: pratik.best | last post by:
Hi, I just seen the web site of the unicode committee and was amazed to see the site showing document in Hindi without using any such fonts like "Kruti Dev" or "Dev Lys". "Webdunia.com" is also showing documents in Hindi without the need to download any specific font. How's that done? Also, can I build such a page?
4
2600
by: Rui Maciel | last post by:
I want to support Unicode on a pet project of mine (small markup language parser). I've read a bit about Unicode (didn't delved beyond the basics) and I searched for some info on how to support Unicode on C programs. Unfortunately I wasn't able to find articles that could be considered more than loose ends, small blog entries and side remarks, never delving too much into specifics. From what I gathered, the two main methods (based on...
8
23909
by: Rui Maciel | last post by:
I've just started learning how to use the wchar_t data type as the basis for Unicode strings and unfortunately I'm having quite a bit of problems, both in the C front and the Unicode front. In this case,it seems that the wprintf function isn't able to print a string beyond the first character. I don't have a clue why this is happening. Here is the test code: <code> #include <stdlib.h>
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10214
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7561
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2935
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.