Newsgroup: it.comp.lang.c
Titolo: what is the difference?

Hi
1. What is the difference between
Multibye character and unbyte character?
2. Which one is being in use now a days.
3. What is better to use in a program
functions like _mbscpy _mbsnbicmp OR strcpy, strncmp etc. Which type of
these functions will support all platform in another word program won't
crash? I am compiling using Microsoft Visual C ++ v6? I want my program
to run on UNIX too.
Thanks
Lee
-- 
Posted via Mailgate.ORG Server - 
http://www.Mailgate.ORG

 to run on UNIX too.
The short answer is: simply use the usual STANDARD string and I/O
functions from <string.h> and <stdio.h> and let the operating system do
the rest.
**********
And now a much looooooooooonger answer. :-)
"Multi-byte encodings" are character encodings which use the byte as the
unit of encoding. Each character is encoded with a variable number 
of bytes, which ranges from a minimum of one 1 to a maximum number
specific to each encoding.
In the C language, a byte is represented by type "char". NOTICE that,
regardless of its name, a "char" is NOT a character: it is a byte.
A multi-byte encoding where the maximum number of bytes per character is 1
is called "single-byte". This kind of encoding can have a maximum of 256
characters, all encoded with a single byte, so it is suitable for
languages written alphabetically (English, Russian, Arabic, etc.).
A multi-byte encoding where the maximum number of bytes per character is 2
is called "double-byte". This kind of encodings normally has 128
characters represented with by a single byte and a maximum of 16.384
characters (128×128) represented by a sequence of two bytes. The best
known example of double-byte encoding is Big-Five, the character set used
in Taiwan.
A multi-byte encoding where the maximum number of bytes per character is 4
is called "quadra-byte". This kind of encodings allow serveral millions
different characters. The only examples of quadra-byte encodings I know
are UTF-8 (a form of Unicode, which represents all Unicode characters
--over one million characters-- using sequences of 1, 2, 3 or 4 bytes) and
GB 18030-2000 (the standard now mandatory in the People's Republic of
China, which represents the same characters as Unicode but with sequences
of 1, 2 or 4 bytes).
The C language does not make any difference between "single-byte",
"double-byte" and "quadra-byte", etc. All of them are just special cases
of "multi-byte". Strings of multi-byte characters are handled with the
usual library functions found in <string.h> and <stdio.h>: strlen(),
strcpy(), fgets(), fprintf(), etcetera.
As long as you use strings as an *atomic* object, there is nothing special
to do support any language. E.g.:
    #include <stdio.h>
    #include <string.h>
    char name [100];
    printf("What's your name?\n");
    scanf(" %s", name);
    printf("Oh, %s, what a nice name!\n", name);
The code above works regardless that user can types his name in Englis,
Russian, Chinese or Japanese characters. If the console's "locale" is
properly configured, this works OK in Linux, in Windows, on Solaris, and
with single-byte, double-byte, quadra-byte encodings...
The problem is when you do NOT use strings as *atomic* object. I.e., when
you want to access the single *characters* inside the string. To access
the characters, you clearly need to know the detail of the multi-byte
encoing. E.g.:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    char name [100];
    int i;
    printf("What's your name?\n");
    scanf(" %s", name);
    printf("Your name is %d characters long.\n", strlen(name));
    for (i = 0; name[i] != '\0'; i++)
       name[i] = (char)toupper(name[i]);
    printf("Your name in uppercase is: %s.\n", name);
The information given by the last printf is NOT necessarily correct.
Imagine that the length of name is 4, and that the console is configured
to a quadra-byte character set: the name could be composed of 4 one-byte
characters, 2 two-byte characters, 1 four-byte character, 2 one-byte
characters followed by 1 two-byte characters, etc.
But the very bad things is using the toupper() function on the single
BYTES of the strings! This can only work on single-byte strings. With all
other types of multi-byte encodings, it will not work properly for
lowercase letters encoded in two or more bytes. In some kinds of encodings
(e.g. the Japanese shift-JIS or the Chinese HZ), it would even CORRUPT the
string.
In order to make it possible to manipulate single characters correctly,
the C standard has introduced the so-called "wide characters", which are
composed of a new type, "wchar_t", and of two new sets of functions:
<wchar.h> and <wctype.h>.
Type "wchar_t" is usually bigger than one byte, so it can contain the
whole code of a character without the need of splitting it into two or
more bytes. E.g., on Windows "wchar_t" is 16 bits (like a "short"), while
on Linux it is normally 32 bits (like a "long").
<wchar.h> contains wide-character clones of the functions in <strings.h>,
<stdio.h> and other headers which work with wide-character strings instead
than the usual wide-characters string. E.g., it contains wcscpy() to
substitute strcpy(), wprintf() to substitute printf(), etc. It also contains
functions to convert multi-byte strings into wide-character strings and vice
versa.
<wctype.h> contains wide-character clones of the functions in <ctype.h>. Eg.,
it contains iswdigit() to substitute isdigit(), etc.
With these new stuff, the code above becomes possible:
    #include <wchar.h>
    #include <wctype.h>
    wchar_t name [100];
    int i;
    wprintf(L"What's your name?\n");
    wscanf(L" %S", name);
    wprintf(L"Your name is %d characters long.\n", wcslen(name));
    for (i = 0; name[i] != L'\0'; i++)
       name[i] = (wchar_t)towupper(name[i]);
    wprintf(L"Your name in uppercase is: %S.\n", name);
Notice the L prefix on wide-character constants and string literals, and
the uppercase "%S" in wprintf() and wscanf().
For more information about character sete, particularly East Asian, Ken
Lunde's book are great, especially this one:
   Ken Lunde,  "CJKV Information Processing", O'Reilly, ISBN: 1565922247
 
http://www.amazon.com/gp/reader/1565922247
(BTW, out of curiosity, why on earth did you come on an *Italian*
newsgroup to ask a question about character sets use in East *Asia*!? :-)
--
Cingar
-- 
questo articolo e` stato inviato via web dal servizio gratuito 
http://www.newsland.it/news
 segnala gli abusi ad 
ab...@newsland.it

 newsgroup to ask a question about character sets use in East *Asia*!? :-)
Maybe because he read the "it" in front of this newsgroup's name as
"Information Technology".


 "Information Technology".
Ah, già, probabile.
Gente, altro che Silicon Valley! altro che Bangalore! l'informatica siamo
noi!
--
Cingar
