B
Brand Bogard
Does the C standard include a library function to convert an 8 bit character
string to a 16 bit character string?
string to a 16 bit character string?
Does the C standard include a library function to convert an 8 bit character
string to a 16 bit character string?
No. All that the C standard knows about char is that it is a -minimum-
of 8 bits long.
i.e., if you must work with 8 and 16 bit character strings you will need
custom routines if you want much portability.
What might interest you, however is:
wchar_t is value superset of char_t, so if you have an array of wchar_t
and copy each member of a char array in the corresponding position in
it, the result will be a valid wchar_t string representing the same text.
also, if setlocale() has been appropriately used then mbstowcs or mbsrtowcs
will convert a string (a sequence of char terminated by a null byte '\0'),
each of which may be part of a multi-byte sequence, into a wide-character
string (a sequence of wchar_t terminated by a wide null byte L'\0').
mbstowcs isn't in out environment, but mbtowc is. Thanks.Haider said:Try mbstowcs it will work.
Brand Bogard said:mbstowcs isn't in out environment, but mbtowc is.
Walter said:No. All that the C standard knows about char is that it is a -minimum-
of 8 bits long.
What might interest you, however is:
wchar_t is value superset of char_t, so if you have an array of wchar_t
and copy each member of a char array in the corresponding position in
it, the result will be a valid wchar_t string representing the same text.
Walter Roberson said:If you have a (narrow) char string, you cannot convert it to
a wchar_t string by setting your locale to "C" and then passing
the string through mbstowcs(). That's because the "C" locale specifies
a -particular- character encoding, and that encoding might not match
the encoding of the execution character set, so mbstowcs() might
map the characters to something unexpected, or could even fail
(if the execution character set happened to use encodings that
were incompatible with the encoding structure for the C locale
character set.)
Thus, in order to convert a char string into a wider string, you
have to copy the chars one by one into an array of wchar_t .
If you need to work with Unicode or utf-16 or whatever after that,
then wcstombs() is what you should look at.
Does anyone have a reference to _how to actually use_ the multi-byte /
wide functions in a real program?
Specifically, I'm looking for a way to read from a text file that is
in one multibyte encoding, manipulate the contents as wide chars, then
write to a text file that is in a _different_ multibyte encoding.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.