Converting wchar_t to char and Vice versa
Hi everyone,
I would appreciate so much help which are different ways for converting an array of chars to an array of wide chars (wchar_t) in plain C and C++.
Thank you some much.
FededS.
[214 byte] By [
FededS] at [2007-11-11 9:53:02]

# 1 Re: Converting wchar_t to char and Vice versa
The widen() algorithm converts char to wchar_t:
whcar_t wa = cin.widen('a');
Of course, you need to use a loop to convert a complete string.
The opposite is accomplished by narrow()
char narrow(char_type c, char deflt) const;
The first argument is the wide-char value that is to be converted to char. If the conversion is possible, narrow() returns the corresponding value of c. Otherwise, it return deflt. Remember that not every wide-char character can be converted to char; wide-char values higher than 255 cannot be represented as char. Therefore, in these cases, narrow returns deflt
Danny at 2007-11-11 20:59:54 >

# 2 Re: Converting wchar_t to char and Vice versa
Thanks so much Danny.
And what about mbtowc(...) function using char and wchar_t pointers?
Fededs
FededS at 2007-11-11 21:00:54 >

# 3 Re: Converting wchar_t to char and Vice versa
mbtowc is quite different. It converts a multibyte string (which is actually a plain char array, except that the "letters" therein may consist of two bytes in some cases, one byte otherwise) to wchar_t. This is not the same as converting char to wchar_t because in char strings every letter is mapped to one byte.
Danny at 2007-11-11 21:01:48 >

# 4 Re: Converting wchar_t to char and Vice versa
Danny, could you explain a little bit further this difference?
Additionally to the widen() algorithm, is there a built-in function that can take two pointer char * and wchar_t * and make the conversion?
Thanks!
FededS at 2007-11-11 21:02:53 >

# 5 Re: Converting wchar_t to char and Vice versa
mutibyte characters, as the name suggests, are encoded as one or two bytes, depending on the specific letter. The problem is that most string related algorithms assume that a character always has a fixed size: 1, 2 or even 4 bytes per character, so strlen() for example has to count bytes to know how many letters a string contains. multibyte strings work differently. Most of the characters occupy the first 7 bytes of a byte, pretty much like ASCII. The 8th bit is usally off. If it's on, it means that the character stretched to the next byte, so you have to read two bytes to know which character is encoded. Because of this peculiarity, multibyte algorithms are more complex, because they first have to tell whether the current character consists of two bytes or just one.
As for widen: it works for a single character but using transform(),http://www.dev-archive.com/getHelpOn/Article/9702
you can easily implement an algoirthm that converts wchar_t strings to char strings and vice versa. The Standard Library actually has a conversion function, but it uses locales, which are overkill for simple translations such as this.
Danny at 2007-11-11 21:03:57 >

